Using JSONP in jquery Ajax limitations Solution _jquery

Source: Internet
Author: User

JSONP solves the problem of Cross-domain Ajax invocation. Why cross Domain Ajax calls? This can be in an application directly in front of the front-end through JS invoke another application (under different domain name) API.
We also use the JSONP in the actual application, but only then knew only one limit of JSONP, can only send GET request, the flaw of the request is the limitation of the length of requests.
Today, the discovery of another limitation of JSONP (in the jquery Ajax scenario)-does not trigger $.ajax's error callback, as follows:

Copy Code code as follows:

$.ajax ({
DataType: ' Jsonp ',
Error:function (XHR) {
This callback function is not executed when an error occurs
}
});

This restriction is determined by the JSONP implementation mechanism.

Workaround:

Using a jquery plugin--jquery-jsonp,https://github.com/jaubourg/jquery-jsonp

Sample code:

Copy Code code as follows:

<script src= "Https://raw.github.com/jaubourg/jquery-jsonp/master/src/jquery.jsonp.js" ></script>

Copy Code code as follows:

$.jsonp ({
URL: ',
Success:function (data) {
},
Error:function (Xoptions, Textstatus) {
Console.log (Textstatus);
}
});

When a JSONP request fails, such as a 404 error, the error callback function executes, and the output string "error".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.