This isn't rocket science, but thought I'd post it since I had trouble finding an example of it…
This demonstrates how to utilize the JSONP support in the Express node.js module.
var express = require("express");
var server = express.createServer();
server.enable("jsonp callback");
server.get("/foo", function(req, res) {
// this is important - you must use Response.json()
res.json("hello");
});
An HTTP GET request to /foo?callback=myfunction would return myfunction("hello");, with the Content-Type header set to text/javascript.
#1 by Clint on August 31, 2011 - 10:27 pm
Thanks for this. Like you said, it’s small, but I just needed to see an example.
#2 by Robert on September 15, 2011 - 11:38 am
Thanks! – Just what I was looking for and I hadn’t found it in the docs.
#3 by Tehsin on December 28, 2011 - 10:29 pm
Hey
Thanks for the example. This is great, but wondering how you would implement a callback for a POST (over cross-domain).
Passing small chunks of data over GET is great, but let’s say if you were passing a nice big array, I am assuming you would do a POST instead of a GET, and when you do that, JSON callback does not work over cross-domain ($.ajax Express). Am I missing something obvious here? I can certainly send POST data using JSON to Express, but if I were to ask then for a callback, that would fail over a different domain because of the domain policy restrictions that JSON would have.
Hoping to figure out a solution to this. Thanks!
#4 by Dirk on February 6, 2012 - 4:52 am
Thanks, very helpful!!
Regarding POST, there is a full roundtrip with a JSONP Ajax callback here:
http://kiwidev.wordpress.com/2011/07/18/node-js-expressjs-and-jsonp-example/
#5 by Martin on February 10, 2012 - 3:26 am
Firstly, just a quick note, the correct way to enable JSONP for Express is -
app.set( “jsonp callback”, true )
(see https://github.com/visionmedia/express/issues/664)
In answer to Tehsin’s question, no, unfortunately, you can’t do a POST with JSONP. Dirk’s example is a GET.
The reason you can’t do a POST is because of the way that JSONP works. JSONP is actually a hack. It takes advantage of the fact that, when you use JS to inject a script node into the page’s DOM, the browser will GET the script using the URL in the new node’s src attribute. The browser always does a GET for this resource and you do not even have control over the HTTP headers used for the request.
#6 by e4r on February 19, 2012 - 5:40 pm
Thanks for the code snipet. It is exactly what I was looking for.
#7 by marcus on April 5, 2012 - 7:35 am
Thank you!!
#8 by carol on September 11, 2012 - 3:01 am
i love u ~ ♡
#9 by Adam Tolley on September 23, 2012 - 1:07 pm
Looks like in Express 3, calling res.jsonp() (explicitly specifying p) is required, even with the option enabled.
I had tried it the other way and was only getting json, even though the request specified a callback. Changing to this fixed it.
#10 by Sapardi on January 3, 2013 - 1:07 am
Thank you so much, simple but useful.
#11 by Lexus on January 21, 2013 - 7:05 am
thanks for this.. it really helped my project.
#12 by JhonSmith on April 26, 2013 - 2:33 pm
Hello!
I’m a software developer Colombia.
I see you are very good at Node.js!
I write because I’m in a test to get a job!
I need to make a form to receive a number and send it via json to the server and the server multiply that number by 2 and return the result, I’ve been part of my project done already but there are some things I do not work and are not because!
I would appreciate if you could help me! It’s urgent!
Thank you!