简体   繁体   中英

What's the reason i have to use jsonp?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM