Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JSONP require server modifications?

I understand that jsonp is a technique to get around the same origin policy. You basically refer to your json serving server endpoint in a script tag, because script tags are exempt from the SO policy.

My question is: Assuming a server has an endpoint that serves up json, are there any modifications necessary on the server to make use of jsonp in the client?

I think no, but want to be sure....

like image 936
hvgotcodes Avatar asked Jul 21 '10 01:07

hvgotcodes


People also ask

What is one reason to avoid using JSONP in a?

JSONP has some other limitations, too: It can only be used for GET requests, and there's no general way to prevent cross-site request forgeries*. It's bad for private data, since any site on the web could hijack a JSONP response if the URL is known.

What does JSONP use for request?

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

Is JSONP deprecated?

Yes, JSONP is obsolete now. There's absolutely no reason to offer a JSONP service anymore.

What is difference between JSON and JSONP?

Json is stardard format that is human readable used to transmit information from one server to another server. Jsonp is a json with ability to transmit information to another domain. JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around it.


1 Answers

Yes, JSONP is slightly different when it renders, so your server needs to support it.

JSON looks like this:

{ "name": "value" } 

Whereas JSONP looks like this:

functionName({ "name": "value" }); 

If whatever you're using supports it you're covered, but it's not the same as supporting just JSON. When the server gets a request, for example: http://example.com/json?callback=functionName, the above is what you should render, because how it looks in the page is this:

<script type="text/javascript" src="http://example.com/json?callback=functionName"></script> 

This means something that runs needs to be returned, as an illustration, this is valid:

<script type="text/javascript">   functionName({ "name": "value" }); </script> 

If your server didn't support JSONP it would effectively be this:

<script type="text/javascript">   { "name": "value" } </script> 

...and you'll get syntax errors, since that's not valid JavaScript.

like image 94
Nick Craver Avatar answered Sep 30 '22 07:09

Nick Craver