How to create a JSONP request, and how to handle a JSONP response.

Has anyone come across this?

And in general, is there a difference between JSON and JSONP?

thank

  • Why do you change the question a year old to another and uncheck my answer? - Firepro
  • one
    @Firepro Received a message that my question causes friction. And what should be reformulated. After that, your answer looked a bit strange. And I acted according to the rules of stackoverflow - Vanya Avchyan
  • what are you speaking about? what friction? Look at the story, your question sounded correct "How to create a JSONP request, and how to handle the JSONP response And is there a difference between JSON and JSONP?" - here the question is correct and the answer to it can be based only on knowledge, and not on opinions, and cannot give rise to endless discussions, since it has a practically unambiguous direction of answer. - Firepro
  • 2
    A radical change to a question that has already been given a good answer is a bad idea. - Nofate

1 answer 1

The difference is definitely there. JSON is a simple data format, and JSONP is a methodology for using this format with cross-domain AJAX requests.

Common JSON :

{"why":"not"} 

JSONP :

 bestFunction({"why":"not"}); 

It turns out that you can use JSONP as a script. The bestFunction function that you wrote earlier will be called, and when it is launched, it will be passed JSON as a parameter. This method is used to provide cross-domain AJAX requests.

Processing might look like this:

 function bestFunction(json){ console.log(json.why); } var elm = document.createElement("script"); elm.setAttribute("type", "text/javascript"); elm.src = "http://test.ru/jsonp"; document.body.appendChild(elm); 

Essentially, you add a script tag to your page, which loads the data in the form of a JavaScript script and makes a callback to your code with the data. Therefore, you can request data from any server, in any browser, without any permissions and additional checks that are present in AJAX requests.