From f2eda1b84e7448f9982f7b6e56522c7d13a95af4 Mon Sep 17 00:00:00 2001 From: keenwon Date: Wed, 7 Sep 2016 16:57:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0debug=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/jsonp.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/jsonp.js b/lib/jsonp.js index 9b0e452..b1fc406 100644 --- a/lib/jsonp.js +++ b/lib/jsonp.js @@ -18,15 +18,29 @@ function jsonp(app, options = {}) { callbackFn = defaultcallbackFn; } + debug(`callback function name: ${callbackFn}`); + app.context.jsonp = function (data) { - var callback = this.query[callbackFn]; + var callback = this.query[callbackFn], + contentType = 'application/json', + response; + + if (this.method !== 'GET') { + debug('the request mehtod is not "GET", return.'); + return; + } - if (this.method !== 'GET' || !callback) { + if(!callback) { + debug(`${callbackFn} is undefined, return.`); return; } - this.type = 'application/json'; - this.body = `${callback}(${JSON.stringify(data)})`; + response = `${callback}(${JSON.stringify(data)})`; + + this.type = contentType; + this.body = response; + + debug(`response: ${response}`); } }