Home  >  Article  >  Backend Development  >  PHP analysis of ajax cross-domain issues

PHP analysis of ajax cross-domain issues

小云云
小云云Original
2018-03-09 13:06:481168browse

In fact, there are many explanations about JSONP on the Internet, but they are all the same and vague. It is a bit difficult for many people who are new to it to understand. It is not a small thing, but I try to explain this problem in my own way. See if that helps. This article mainly shares with you PHP's analysis of ajax cross-domain issues, hoping to help everyone.

1. A well-known problem, Ajax direct request for ordinary files has the problem of cross-domain unauthorized access. Regardless of whether you are a static page, dynamic web page, web service, or WCF, as long as it is a cross-domain request, it will not be allowed. Accurate;

2. However, we also found that when calling js files on a Web page, it is not affected by whether it is cross-domain (not only that, we also found that all tags with the "src" attribute have cross-domain capabilities, such as 3f1c4e4b6b16bbbd69b2ee476dc4f83a, a1f02c36ba31691bcfe87b2722de723b, d5ba1642137c3f32f4f4493ae923989c);

3. Therefore, it can be judged that if you want to use pure web side (ActiveX control, server-side proxy, etc.) at the current stage, it belongs to the future There is only one possibility to access data across domains (excluding HTML5 Websocket and other methods), and that is to try to load the data into a js format file on the remote server for client calling and further processing;

4. We happen to already know that there is a pure character data format called JSON that can describe complex data concisely. What’s even better is that JSON is also natively supported by js, so the client can process data in this format almost as desired;

5. In this way, the solution is ready. The web client calls the js format file dynamically generated on the cross-domain server (usually with JSON as the suffix) in exactly the same way as the calling script. It is obvious that the reason why the server To dynamically generate a JSON file, the purpose is to load the data needed by the client into it.

6. After the client successfully calls the JSON file, it will obtain the data it needs. The rest is to process and display it according to its own needs. This way of obtaining remote data looks like Very much like AJAX, but not the same.

7. In order to facilitate the client to use data, an informal transmission protocol has gradually formed. People call it JSONP. One of the key points of this protocol is to allow users to pass a callback parameter to the server, and then the service When the client returns data, it will use this callback parameter as a function name to wrap the JSON data, so that the client can customize its own function to automatically process the returned data.

The original ajax in the front-end code is

 $.ajax({
             type: "get",
             async: false,
             url: "url",
             dataType: "json",             success: function(json){
                 alert('success');
             },
             error: function(){
                 alert('fail');
             }
         });

and should be changed to

 $.ajax({
             type: "get",
             async: false,
             url: "url",
             dataType: "jsonp",
             jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)             jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据             success: function(json){                alert('success');             },
             error: function(){
                 alert('fail');
             }
         });


##Compare the difference between json and jsonp formats

json format:


{    "message":"获取成功",    "state":"1",    "result":{"name":"工作组1","id":1,"description":"11"}
}


jsonp format:


callback({    "message":"获取成功",    "state":"1",    "result":{"name":"工作组1","id":1,"description":"11"}
})


You can see the difference. In the URL, the parameter of callback passed to the background is Shenma. Callback is Shenma. Jsonp has one more layer than json, callback(). So you know how to deal with it. So modify the background code.

So the original echo json_encode($xxx) in php must be changed to

$callback = $_GET['callback'];echo $callback.'('.json_encode($xxx).')';

Related recommendations:


The best way to solve ajax cross-domain Full solution

Ajax cross-domain perfect solution example sharing

JS implements Ajax cross-domain request flask response content

The above is the detailed content of PHP analysis of ajax cross-domain issues. For more information, please follow other related articles on the PHP Chinese website!

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