4

What's the reason i have to use jsonp?

A few days ago i asked why i have no response from a rest server with jquery. The reason was that i must use JSONP. I tested that with a own server and it worked.

Now i have to convince my college's who have control of the right server that the output have to be JSONP instead of json.

Only i don't now exactly why i must use JSONP? And is this only a jquery problem or is it not possible with javascript at all?

Can anyone help me with these questions? Thanks

4
  • 3
    en.wikipedia.org/wiki/Same_origin_policy#Workarounds
    – j08691
    Commented Mar 5, 2012 at 22:20
  • have a look at the Same Origin Policy en.wikipedia.org/wiki/Same_origin_policy
    – reedlauber
    Commented Mar 5, 2012 at 22:20
  • 1
    you can use a proxy...don't expect other API's to change for your convenience
    – charlietfl
    Commented Mar 5, 2012 at 22:28
  • 1
    It's not just a jQuery thing, it's the way JavaScript works. But you can have your JS make an Ajax call to your own web server (i.e., stay in the same domain) and then have your web server make the request to the other server to get the JSON.
    – nnnnnn
    Commented Mar 5, 2012 at 22:34

2 Answers 2

8

JSONP is used to get data via AJAX cross-domain. Well, not exactly, JSONP is actually a bit of a "hack".

AJAX requests only work on the same domain, but <script> tags can be included from any domain. This is what JSONP is, it's actually a Javascript file, that gets added as a <script> tag.

That's why in JSONP, it's callback({data: value}), this is a script that gets executed.

1
  • Thank you that will help me with convince them!!
    – robind38
    Commented Mar 5, 2012 at 22:43
5

If the AJAX request is being made to an URL that falls under the so called Same origin policy, it will normally fail in most browsers due to built-in browser restrictions.

But if you are on the same domain, protocol and port as your colleges server, you don’t need JSONP to make AJAX requests, you can just go ahead using the standard AJAX tools.

If you are not, JSONP is an industry-standard technique of working around the same origin policy, but it also requires that the server delivers data in a special way to make it available for the client.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.