What causes Ajax to be unable to cross-domain request issues?
Ajax itself actually interacts with the data through the XMLHttpRequest object, and the browser does not allow the JS code to cross-domain operations for security reasons, so it warns. Cross-domain security restrictions are referred to as browser-side, there is no cross-domain security restrictions on the server side. So for these 2 kinds of situations derive 2 types of cross-domain solutions, one is the server-side to do relay-like proxy way, a class of JS processing browser side of the real cross-domain access.
<script type= "Text/javascript" src= "./jquery-1.6.4.min.js" ></script> <script type= "Text/javascript" >functionShort () {varurl_long=$ ("#url_long").Val (); varsource=$ ("#source").Val (); varRequest = "http://api.t.sina.com.cn/short_url/shorten.json?url_long=" +url_long+ "&source=" +source+ "& Callback=? "; //&callback=? must be added, myurl?callback=? "JQuery will be replaced automatically? for the correct function name to execute the callback function. The jquery API documentation is described. $.Ajax ({dataType: "Jsonp",//cross-domain access DataType must be of type JSONP. Url:request,type: "GET",Jsonp: "Callback",//The parameter name that is passed to the request handler or page to obtain the name of the JSONP callback function (generally by default: callback)Jsonpcallback: "Flighthandler",//Custom JSONP callback function name, default to jquery automatically generated random function name, can also write "?", jquery will automatically process the data for youSuccessfunction(response) {$ ("#shortUrl"). HTML ("Short address:" +response[0].url_short); },Error:function(XMLHttpRequest, Textstatus,Errorthrown) {Alert ("Status" +xmlhttprequest.status); Alert ("ReadyState" +xmlhttprequest.readyState); Alert ("Textstatus" +textstatus); alert (Errorthrown); } }); return false; } </script> });
PHP Backend:
Ult = Json_encode (Array($data));Echo"Flighthandler ($result)";//The Flighthandler here is the name defined in Jsonpcallback above $ajax, since jquery is already encapsulated JSONP can be taken directly. When using JSONP, the function is called with JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? is the correct function name to execute the callback function. It is suggested that the function names here can be obtained dynamically, in order to implement different code examples:$method=isset($_get[' method '])?Trim($_get[' method ']): ' Flighthandler ';//Get method Name$result= Json_encode (Array($data));Echo $method." ($result)";
Or
Print_r ($_geturldecode(Json_encode ($rs _info_arr)). ‘)‘);
Note two points:
1. The datatype must be JSONP
2.callback=? It has to be added that the success:function has not responded (returning data).
3.charset= "Utf-8" is utf-8, the file should be saved in the format encoding utf-8.
If you submit using a form form, you do not need to consider cross-domain issues.
meta charset=" Utf-8 "> <title>Tools</title> Welcome to the Address transliteration tool, please enter the link <br><br> <form action= " Http://api.t.sina.com.cn/short_url/shorten.json "method=" get "> Item Link:<input type=" string "Name=" Url_ Long "/><br> APIkey <input type=" string "name=" source "value=" 1681459862 "/><br ><br> <input type= "Submit" name= "Submit" value= "commit"/> </form> </div></ Body>
About jquery cross-domain request PHP data