当success
服务器响应时调用回调方法。该方法通过调用回调方法$.ajax
设置一个处理响应的函数。success
未调用该success
方法的最可能原因是来自服务器的响应不正确。该$.ajax
方法在查询字符串中发送一个值,callback
服务器应将其用作 JSONP 响应中的函数名。如果服务器使用不同的名称,则永远不会调用该$.ajax
方法设置的函数。
If the server can not use the value in the callback
query string to set the function name in the response, you can specify what function name the $.ajax
method should expect from the server. Add the property jsonpCallback
to the option object, and set the value to the name of the function that the server uses in the response.
If for example the $.ajax
method is sending a request to the server using the URL http://service.mydomain.com/getdata?callback=jsonp12345
, the server should respond with something looking like:
jsonp12345({...});
If the server ignores the callback
query string, and instead responds with something like:
mycallback({...});
Then you will have to override the function name by adding a property to the options object:
$.ajax({
url: urlOnDiffDomain,
dataType: 'jsonp',
data: {},
success: function(data, textStatus) {
alert('success...');
},
jsonpCallback: 'mycallback'
});