Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

issue #1190 : renaming jsonp callback #1213

Closed
wants to merge 5 commits into from

4 participants

@riadhchtara

No description provided.

lib/response.js
@@ -188,12 +188,21 @@ res.json = function(obj){
, replacer = app.get('json replacer')
, spaces = app.get('json spaces')
, body = JSON.stringify(obj, replacer, spaces)
- , callback = this.req.query.callback;
+ , callbackname = app.get('jsonp callback name')

You could do pretty much all of this in one line:

  , callback = this.req.query[app.get('jsonp callback name') || 'callback']

I change it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/response.js
@@ -188,12 +188,12 @@ res.json = function(obj){
, replacer = app.get('json replacer')
, spaces = app.get('json spaces')
, body = JSON.stringify(obj, replacer, spaces)
- , callback = this.req.query.callback;
+ , callback = this.req.query[app.get('jsonp callback name') || 'callback'];
@tj
tj added a note

we could even kill the || callback operation which will be the most common use-case and just add:

app.set('jsonp callback name', 'callback');

in application.js https://github.com/visionmedia/express/blob/master/lib/application.js#L84

I dont think many people will be changing it

I have just fix that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 4, 2012
  1. @riadhchtara
  2. @riadhchtara

    cade refartoring

    riadhchtara authored
  3. @riadhchtara

    delete space

    riadhchtara authored
Commits on Jul 6, 2012
  1. @riadhchtara

    Update Issue1190

    riadhchtara authored
  2. @riadhchtara

    Update Issue1190

    riadhchtara authored
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 3 deletions.
  1. +1 −0  lib/application.js
  2. +2 −3 lib/response.js
  3. +17 −0 test/res.json.js
View
1  lib/application.js
@@ -84,6 +84,7 @@ app.defaultConfiguration = function(){
// default configuration
this.enable('jsonp callback');
+ this.set('jsonp callback name', 'callback');
this.configure('development', function(){
this.set('json spaces', 2);
View
5 lib/response.js
@@ -1,4 +1,3 @@
-
/**
* Module dependencies.
*/
@@ -188,12 +187,12 @@ res.json = function(obj){
, replacer = app.get('json replacer')
, spaces = app.get('json spaces')
, body = JSON.stringify(obj, replacer, spaces)
- , callback = this.req.query.callback;
+ , callback = this.req.query[app.get('jsonp callback name')];
// content-type
this.charset = this.charset || 'utf-8';
this.set('Content-Type', 'application/json');
-
+
// jsonp
if (callback && jsonp) {
this.set('Content-Type', 'text/javascript');
View
17 test/res.json.js
@@ -22,6 +22,23 @@ describe('res', function(){
})
})
+ it('should allow renaming callback', function(done){
+ var app = express();
+
+ app.set('jsonp callback name', 'clb');
+ app.use(function(req, res){
+ res.json({ count: 1 });
+ });
+
+ request(app)
+ .get('/?clb=something')
+ .end(function(err, res){
+ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
+ res.text.should.equal('something({"count":1});');
+ done();
+ })
+ })
+
it('should allow []', function(done){
var app = express();
Something went wrong with that request. Please try again.