JSONP: Is a callback necessary?

后端 未结 4 417
说谎
说谎 2021-01-21 17:08

I\'m creating an iOS / Droid app using AJAX, jQuery, and Phonegap. The backend is a Drupal 7 site serving content via web services. I\'m a little confused about the necessity fo

相关标签:
4条回答
  • 2021-01-21 17:14

    Yes, it's required. Default function name is callback, but can be any declared function name.

    https://jar-download.com/java-documentation-javadoc.php?a=jersey-json&g=com.sun.jersey&v=1.19.2

    It provides us with a way to access the returned data. It does this by having the server return JSON data wrapped in a function call (the “padding”) which can then be interpreted by the browser. This function must be defined in the page evaluating the JSONP response.

    see here

    0 讨论(0)
  • 2021-01-21 17:17

    JSONP was desinged in response to the Same-Origin Policy (SOP) which stated that if the HTML page was served from one domain, then the web page (once delivered to a client) could not make an "Ajax call" to a site on a different domain. If the "Ajax call" was made to the same domain, all is fine. (Perhaps that it was you are seeing?)

    Now, you can't make Ajax calls to different domains but you can use the script tag to invoke code on a different domain (go figure, eh?). Now the thing is, suppose you made a call to a different domain in your script tag and you got back some JSON text. How would you do anything with it? This is where JSONP comes in. If the server sends back some JSON wrapped in a function call, then when you evaulate that (wrapped) object, you are now "doing something with it."

    Lately though, most browsers support CORS, so JSONP is not needed. Some older browsers do not support CORS, however, but these are getting to be fewer and fewer.

    0 讨论(0)
  • 2021-01-21 17:30

    A JSONP call doesn't work without a callback. The data is loaded in a script tag, and if the code is not in a form of a method call, the result would just be an object that was discarded, and the success callback method would never be called.

    The ajax method is adding a callback parameter to the URL even if you don't specify one.

    In the documentation, under the "jsonp" value for the dataType setting:

    "Adds an extra "?callback=?" to the end of your URL to specify the callback."

    http://api.jquery.com/jQuery.ajax/

    0 讨论(0)
  • 2021-01-21 17:34

    Is a callback necessary?

    Yes, definitely. The callback is substantial to the definition of JSON-with-padding. Without, it is just JSON.

    0 讨论(0)
提交回复
热议问题