Home  >  Article  >  Web Front-end  >  Solutions to the limitations of using jsonp in jquery ajax_jquery

Solutions to the limitations of using jsonp in jquery ajax_jquery

WBOY
WBOYOriginal
2016-05-16 17:13:081127browse

jsonp solves the problem of cross-domain ajax calls. Why do we need to make cross-domain ajax calls? In this way, one application can directly call the API of another application (under a different domain name) through js on the front end.
We have also used jsonp in practical applications, but we only knew one limitation of jsonp before. It can only send get requests. The disadvantage of get requests is that the request length is limited.
Today, I discovered another limitation of jsonp (in the context of jquery ajax) - the error callback of $.ajax will not be triggered. The sample code is as follows:

Copy code The code is as follows:

$.ajax({
dataType: 'jsonp ',                                                                                                                                                                                                                                                                                  .
This limitation is determined by the implementation mechanism of jsonp.

Solution:

Use a jquery plugin - jquery-jsonp, https://github.com/jaubourg/jquery-jsonp
Sample code:

Copy code


The code is as follows:



Copy code

The code is as follows:

$.jsonp({ url: '' , success: function (data) { }, error: function (xOptions, textStatus) {
console.log(textStatus);
}
});



When there is an error in the jsonp request, such as a 404 error, the error callback function will be executed and the string "error" will be output.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn