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

Solutions to the limitations of using jsonp in jquery ajax_jquery

WBOY
Release: 2016-05-16 17:13:08
Original
1162 people have browsed it

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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template