2

Possible Duplicate:
json Uncaught SyntaxError: Unexpected token :

Does jsonp depend on server support, in order to return a correct response?

I got this request, and what I get back is js error.

$.getJSON('myURL?q=keyword&callback=?', function(data){console.log(data)});

even angular jsonp method isn't working:

http://jsfiddle.net/neoswf/tckGG/5/

console >> js error: Uncaught SyntaxError: Unexpected token :

Does jsonp depends on some server implementations?

SOLVED!!!

This question is different from others cause no other answers speak about the proxy solution.

Community
  • 1
  • 1
neoswf
  • 4,730
  • 6
  • 39
  • 59

3 Answers3

3

The server doesn't return JSONP, it returns JSON. A JSONP result is a JSON result wrapped in a function call.

Example:

callback873659823745({"destino":"http://...", ... })

If you can't make the server return JSONP, you would need a proxy server that requests the service and returns the JSON result wrapped as a JSONP result.

As it happens, I set up such a proxy a while ago, at jsonp.guffa.com, that you could use if you don't have the ability to set up one yourself.

Demo: http://jsfiddle.net/Guffa/tckGG/6/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • @Guffa- how can I write one of my own? If I'll be using your proxy, I'm afraid it might be too much expensive for you... – neoswf Oct 10 '12 at 23:46
  • Never mind. Already found few tutorials on the web... http://blog.jsonpify.com – neoswf Oct 10 '12 at 23:50
  • But how did you manage to bypass the Origin Policy using your proxy? After all I made a normal get request and got denied by their server. – neoswf Oct 11 '12 at 01:10
  • 1
    @NeoSwf: Same-origin policy only applies to scripts executed in browsers. – Felix Kling Oct 11 '12 at 02:40
2

Yes, jsonp requires server support. The server needs to wrap the json in the "padding" part of the json*P*, which is a method that the client defines and executes around the response data.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • is there a way I can bypass it? if not, and if the server have not implemented CORS, than do I got any option achieving my goals? – neoswf Oct 10 '12 at 22:43
  • 1
    @NeoSwf: You have to use a proxy... – Felix Kling Oct 10 '12 at 22:45
  • Thank you @FelixKling. Can you explain? How can the proxy will not invoke cross domain rules? – neoswf Oct 10 '12 at 22:54
  • @NeoSwf: You have to run it from your domain, i.e. create an endpoint which accepts a URL, connects to that URL and returns the response to your script. – Felix Kling Oct 10 '12 at 23:47
2

Because your Url isn't returning JSONP. It is returning JSON.

{"destino":"http://www.buscape.com.br","palavras":["tv lcd 32","tv led","tv led 32","tv led 40","tv","tv led 42","tv lcd","tv lcd 42","tv lcd 26","tv 3d","tv lcd 40","tv 32","tv led 46","tv 42","tv lcd 22","tv led 32 full hd"]}

For an example if JSONP, check the return value from flickr. It returns

callbackToExecute({... JSON Object ...})
amit_g
  • 30,880
  • 8
  • 61
  • 118