Home > Web Front-end > JS Tutorial > body text

Examples to explain four methods of AJAX cross-domain request data

小云云
Release: 2017-12-19 13:54:48
Original
2042 people have browsed it

Due to the browser's same-origin policy, ajax requests cannot receive data returned from the request response. This article mainly brings you four methods of AJAX cross-domain request data, hoping to help you.

Requesting data requires calling the browser's built-in constructor XMLHttpRequest() to create an instance object

var xhr = new XMLHttpRequest();

Note that ActiveXobject is supported before IE8 ("Microsoft.XMLHTTP"); Remember to perform compatibility processing. I won't write it here.

Get through this object

The four states of obtaining data Saves several states of request data

1.xhr.open(request method, request address, setting synchronization/asynchronous);

2.xhr.send(null);//Send Request If it is a post request, the parameters need to be passed in send

3. It will always check whether the data is returned

4. If the data is returned, the value of xhr.readyState is 4, indicating that the data is returned successfully

So how do we make a cross-domain request?

Option 1:

Use the src attribute of the script tag to request data. The data requested by the src attribute will not be intercepted by the browser. The

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!