From 48613b7f0be58b1006ed55d18bbbfb0af935ae4e Mon Sep 17 00:00:00 2001 From: welefen Date: Wed, 20 Nov 2013 11:28:58 +0800 Subject: [PATCH] fix #3. auto send content-type header. --- lib/Conf/convention.js | 1 + lib/Lib/Core/Controller.class.js | 9 ++++++++- lib/Lib/Core/View.class.js | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Conf/convention.js b/lib/Conf/convention.js index 832cb4c5..9ef0ed47 100644 --- a/lib/Conf/convention.js +++ b/lib/Conf/convention.js @@ -27,6 +27,7 @@ module.exports = { deny_remote_access_by_ip: true, //禁止通过外网的IP直接访问 url_callback_name: "callback", //jsonp格式的callback名字 json_content_type: "application/json", + auto_send_content_type: true, //是否自动发送Content-Type,默认值为`tpl_content_type`配置值 cookie_domain: "", //cookie有效域名 cookie_path: "", //cookie路径 diff --git a/lib/Lib/Core/Controller.class.js b/lib/Lib/Core/Controller.class.js index 2a37f023..81599416 100644 --- a/lib/Lib/Core/Controller.class.js +++ b/lib/Lib/Core/Controller.class.js @@ -214,7 +214,9 @@ module.exports = Class(function(){ * @return {[type]} [description] */ json: function(data, jsonp){ - this.header("Content-Type", C("json_content_type")); + this.header("Content-Type", C("json_content_type") + ";charset=" + C("charset")); + //标记content-type的header已经发送 + this.http.sendContentType = true; var callback = this.get(C("url_callback_name")); callback = callback.replace(/[^\w\.]/g, ""); if (jsonp && callback) { @@ -233,6 +235,11 @@ module.exports = Class(function(){ * @return {[type]} [description] */ echo: function(obj, encoding){ + //自动发送Content-Type的header + if (C('auto_send_content_type') && !this.http.sendContentType) { + this.header("Content-Type", C("tpl_content_type") + ";charset=" + C("charset")); + this.http.sendContentType = true; + }; if (is_array(obj) || is_object(obj)) { obj = JSON.stringify(obj); }; diff --git a/lib/Lib/Core/View.class.js b/lib/Lib/Core/View.class.js index 82561656..9f6019c7 100644 --- a/lib/Lib/Core/View.class.js +++ b/lib/Lib/Core/View.class.js @@ -27,7 +27,7 @@ module.exports = Class(function(){ content = this.fetch(templateFile, content); this.render(content, charset, contentType); tag("view_end", this.http); - this.http.res.end(); + this.http.end(); }, render: function(content, charset, contentType){ if (charset === undefined) {