Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON_CALLBACK not found using JSONP

I am trying to get a JSONP response working using the following code...

$http.jsonp('http://example.com:8888/?callback=JSON_CALLBACK').success( function( response )
{
    console.dir( response );
});

http://example.com:8888/?callback=JSON_CALLBACK returns the following via node.js

JSON_CALLBACK({ date:'2013-05-15T08:53:51.747Z' });

The header is being set in node.js like this....

res.writeHead(200, {'Content-Type': 'application/json'});

Yet the error I get in a Chrome console is this...

Uncaught ReferenceError: JSON_CALLBACK is not defined 

However, weirdly, If I create the function window.JSON_CALLBACK(response) it will run. But I thought that success is meant to do that on my behalf.

like image 796
thomas-peter Avatar asked Nov 02 '22 22:11

thomas-peter


1 Answers

Your content-type header is not correct.

Use application/json if what you return is plain JSON. JSONP is Javascript, so you should use application/javascript

like image 96
Andre Goncalves Avatar answered Nov 15 '22 13:11

Andre Goncalves