AJAX can obtain server data without refreshing a new page. However, due to the security restrictions of various browsers, Ajax is likely not to allow cross-origin access (this type of restriction is necessary. without any restriction, AJAX can use "file: // XXX "obtains important information of the local host and sends it to the server, causing security risks ).
However, sometimes, if a page located in Domain A (assuming the domain name is a.com) does need to access Domain B (assuming the domain name is B .com) and obtain some data, is there a way to achieve this?
The traditional method is to process data on the server side of Domain A, that is, the client accesses a page of domain a.com, and a.com accesses Domain B through the HTTP protocol on the server side, and then returns the data to the client. The a.com server acts as a proxy.
The disadvantage of this method is that the whole page is refreshed every time you access the.com page. The following scenario is not very well-handled: B .com is time-consuming in processing requests. It allows customers (including a.com and browser clients) to return requests immediately, however, you need to keep polling to query the processing progress. Since the B-S architecture does not support the server to actively send messages to the client, it is very common to use Ajax Round Robin to simulate this behavior. If we use the method (a.com server as the proxy) mentioned earlier, the whole page will be refreshed during each round of inquiry and the user experience will be poor. The best solution is to directly access B .com in Ajax.
Ajax cannot be accessed across domains. That is to say, the pages generated by the.com server cannot be accessed directly through Ajax. There is a clever way to bypass this security mechanism:
We noticed that the script tag and src attribute of HTML can be used to determine the source of JS files across domains, that is, the following code is legal
<script src="http://b.com/xxx.js"></script>
This code can be correctly loaded to the JS file in B .com and executed even on the page generated by a.com.
If developers of a.com and B .com can communicate with each other, or B .com explicitly supports the Conventions mentioned below, AJAX can use this mechanism, obtain data on B .com (these restrictions prevent the following methods from being considered a serious security vulnerability ). The specific method is:
- A.com page dynamically generate a script element, src attribute is set to B .com to get Data URL, such as http:// B .com/xxx? XXX, at the same time, add a special parameter at the end of this resource URL to become a http:// B .com/xxx? Xxx & callback = fun. Note that the callback is a convention name (the callback name parameter must be specially processed on the B .com server ). That is to say, you can change this parameter name to any name such as mycallback on the a.com page, but you must tell B .com what the name is. The callback value "fun" is the name of a function implemented on the.com page.
- The script allows cross-origin access. a.com will apply for the JS file (CODE) corresponding to this URL from B .com ). B .com obtains the URL, extracts the callback parameter, and obtains the string "fun.
- B .com dynamically generates JS files (CODE), and inserts a piece of code "Fun (...)". A parameter is usually a JSON object (this is the Convention, a.com and B .com can also contract other parameter types), such as "Fun ({p1: 1, P2: 2 })". B .com sends the JS file (CODE) containing the Code to a.com.
- After the script tag of a.com obtains the JS file (CODE) from B .com, it is executed immediately. At this time, "Fun ({p1: 1, P2: 2}) is executed }) "This code. Note that fun is a function on the.com page. Here it is a function call. Now, the fun function of a.com can receive the JSON object ({p1: 1, P2: 2}) transmitted by B .com.
Jquery is also implemented using a similar mechanism. An example is as follows:
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){ $.each(data, function(i,item){ $("p").append(i+": "+item+"<br>"); }); });
Note that the called URL contains "jsoncallback =? ","? "Will be replaced by jquery with the actual callback function name, and users (web page writers) do not have to worry about it. When the cross-origin server responds, jquery ensures that the anonymous function compiled by the user can be called (the second parameter of $. getjson () in the example ).
If the server is JSP, the code is roughly as follows:
...String jsoncallback=request.getParameter("jsoncallback");...PrintWriter out = response.getWriter();out.print(jsoncallback+"({\"account\":\"XX\",\"passed\":\"true\",\"error\":\"null\"})");
The data obtained by jquery may be as follows:
JQUET0988788({"account":"XX","passed":"true","error":"null"})
Jquet0988788 calls an anonymous function compiled by the user.
[Tips]
The IE kernel browser is using scripts. when the SRC method requests resources, the request does not carry the sessionid information. For the server side, each request comes from a different client, in the Ajax round-robin scenario, the server may be unable to accurately determine the identity of the client, making it difficult to implement the requirement. Firefox, chrome, and other browsers do not have this problem (their requests can carry sessionid ).
Solution: the client is in the script. add special parameters to the SRC request URL to carry the sessionid (similar to the preceding callback parameter, which is agreed with B .com). If the client has not obtained the sessionid, this parameter can be left unspecified; the server obtains this sessionid to identify the client identity. If no sessionid is specified in the URL, the automatically generated sessionid (request. getsession (). GETID () and add it to a field of the returned JSON object. The client obtains the value of this field and adds it to the URL of the next request.