I am creating a simple JSON API that just returns me an object using jsonp using node-js
Here is the code for server side :
app.get('/vit',function (req,res,next) {
res.type('application/json');
res.jsonp(items); //items is the object
});
On deploying to nodejitsu , and going to url /vit i get the object items.That means its working on the server side.
I have another domain from where I wanna get this object using jsonp
Heres the client-side code :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$.getJSON('http://trial.jit.su/vit',function (data) {
console.log(data) ;
});
</script>
But I get the following error on the console:
XMLHttpRequest cannot load http://trial.jit.su/vit. Origin http://jquer.in is not allowed by Access-Control-Allow-Origin.
Seems like I have not understand Jsonp.
Quoting the jQuery docs
http://api.jquery.com/jQuery.getJSON/
You need a query string param with a question mark as placeholder.
And looking at the response of http://trial.jit.su/vit you’re missing the callback function. You’re just returning plain JSON without the P, e.g. if the url contains
?callback=baconyour response needs to beBut express will do this for you, as long as you supply a
callbackparam.So just change the following: