Home  >  Article  >  Web Front-end  >  jQuery implements cross-domain_jquery

jQuery implements cross-domain_jquery

WBOY
WBOYOriginal
2016-05-16 16:16:001282browse

I have seen cross-domain implementation using jsonp before, but I have never used it. Something is coming right now. I have tried many methods online, but to no avail. Finally I figured out how to use it and recorded it.

Client:

Copy code The code is as follows:


$(document).ready(function(){
$.ajax({
        type: 'GET',
async: false,
  url: remote_url,
DataType: 'jsonp',
         jsonp: 'callback',
         jsonpCallback: 'fun',
          data: {a: 'b'},
         sucess: function(json) {
alert(json);
}
});
});
function fun(json) {
alert(json);
}

Server side

Copy code The code is as follows:

header('Content-Type: application/json; charset=utf-8');//Output header
//your code
echo $GET['callback'] . '(' . json_encode($GET) . ')';

Be sure to call the callback function in the client js, otherwise an error will occur.

Regarding js spanning, the above method requires the cooperation of the server and outputs the callback function.

If you need to visit other people’s websites to grab something, in addition to using iframe, if you need to use js, how to deal with it.

We know that there is no cross-domain problem when the server accesses remote links. Therefore, we can take a detour and use js to access our local program and access our target URL in the program. This is a new idea, you can try it if necessary.

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