jquery jsonp异步请求,success和jsonpCallback的区别
1个回答
展开全部
$.ajax({
url:
type: ‘GET’,
dataType: ‘jsonp’,
success:getGoodsList
});
$.ajax({
url:
type: ‘GET’,
dataType: ‘jsonp’,
jsonpCallback:getGoodsList
});
success
Type: Function( Anything data, String textStatus, jqXHR jqXHR )
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
jsonpCallback
Type: String or Function()
Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it’ll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.
循环调用ajax的时候,由于循环的结果参数都在变化,如果回调函数需要获取调用ajax时作用域空间的值,则使用success,
如果使用jsonpCallback,获取值可能会混乱。
url:
type: ‘GET’,
dataType: ‘jsonp’,
success:getGoodsList
});
$.ajax({
url:
type: ‘GET’,
dataType: ‘jsonp’,
jsonpCallback:getGoodsList
});
success
Type: Function( Anything data, String textStatus, jqXHR jqXHR )
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
jsonpCallback
Type: String or Function()
Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it’ll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.
循环调用ajax的时候,由于循环的结果参数都在变化,如果回调函数需要获取调用ajax时作用域空间的值,则使用success,
如果使用jsonpCallback,获取值可能会混乱。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询