Ajax request data across domains

Source: Internet
Author: User

Recently started to contact Ajax cross-domain request problem, compared to the network said a lot of, I said here is more superficial.

On the question of why cross-domain, the actual requirement is that when the site project is deployed on a domain name, the domain can be a good solution to the problem of Web site lag (having multiple servers is another matter).

This practice is now very common, look at a few sites more carefully can see the doorway.

Compare the Ajax requests with the cross-domain requests before the cross-domain:

Before:

function (data) {                alert (data);                Window.location.reload ();            });

Here is the Post method (generally more convenient, but not proficient in Ajax is not recommended to start with this said to use), you can also use get.

To say, cross-domain requests do not support post mode! Nor does it mean that there is no support at all, using a POST request when doing cross-domain restrictions on the encoding type, allowing only content-type to

One of application/x-www-form-urlencoded, Multipart/form-data, or Text/plain.

........

After:

  $.ajax ({type:  "GET"  false   Url:wwwurl + "/home/index" " Jsonp " " callback ", //  to the request handler or page, identify the JSONP callback function name (typically: callback).  Jsonpcallback: function  () {}, // callback.  Success: function  

Here you can compare the differences between the two ways.

The Jsonpcallback property is followed by an anonymous function, in fact, this should be called from another "domain" returned function name, function in the domain JS inside, return back directly call on the line, remember to take the argument!

The service side of the code will not show, it is passed a callback parameter validation is empty, the conditional return to a JSON opposite, containing the operation of the data returned when the state message,success inside the call is data. Message.

Baidu Library has a detailed description of the relevant, do not repeat it here.

Ajax request data across domains

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.