I have a function like
User.prototype._send = function(type, code, message, callback) {
if(!message && typeof code != 'number') {
callback = message;
message = code;
code = 200;
}
if(typeof message != 'string')
message = JSON.stringify(message);
if(type == 'connection' && this.connection) {
this.connection.writeHead(code || 200, {
'Content-Type': 'application/json',
'Content-Length': message.length
});
this.connection.end(message);
} else {
if(!this.listeners.length)
return this.message_queue.push(arguments);
var cx = this.listeners.slice(), conn;
this.listeners = [];
while(conn = cx.shift()) {
conn.writeHead(code || 200, {
'Content-Type': 'application/json',
'Content-Length': message.length
});
conn.end(message);
}
if(callback) callback();
}
};
It returns JSON to the client now. But I want it to return JSONP. I did a lot of research and tried to replace .end
with .jsonp
but it does not work.
JSONP("JSON with padding") is a communication technique and is not an another object notation. See http://en.wikipedia.org/wiki/JSONP for more details.
Basically your application need to accept the query parameter jsonp
and wrap the json message with that parameter or callback as shown below
var jsonpCallback = req.query.jsonp; //Assuming you are using express
message = JSON.stringify(message);
message = jsonpCallback + "(" + message + ");"