Abort JSONP ajax request with jQuery

前端 未结 5 1147
说谎
说谎 2020-12-28 19:36

I\'m using a JSONP ajax call to load some content from a different domain, and all this stuff is executed if the user causes a \"mouseover\" on a button.

I can captu

5条回答
  •  没有蜡笔的小新
    2020-12-28 20:05

    jsonpString overrides the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the URL.

    So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the ?callback string to the URL or attempting to use =? for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

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

    jQuery adds ?callback=jQuery17109492628197185695_1339420031913 and later sets the request data as parameter to this callback, so you will have:

    jQuery17109492628197185695_1339420031913({
      "status": 200,
      "data": {your json}
    })
    

    To avoid setting additional parameters to request a URL and calling callback, add this parameter to ajax method: jsonp:false, so it will be look like:

    $.ajax({
      ...
      jsonp:false,
      ...
    });
    

提交回复
热议问题