This article mainly introduces the JS Cross-domain request in 5 solution of the relevant data, the need for friends can refer to the -->
Cross-domain Request data solutions are mainly the following solutions:
?
| 1 2 3 4 5 |
Jsonp mode Form Post way Server Agent HTML5 xdomainrequest Flash Request |
Separate description:
First, JSONP:
Intuitive Understanding:
is to register a function dynamically on the client
function A (data), and then the functions are passed to the server, the server returns a ({/*json*/}) to the client run, which calls the client's
function A (data), which enables Cross-domain.
Birth background:
1, Ajax direct request common files exist Cross-domain access to the problem, no matter is static pages, dynamic Web pages, Web services, WCF, as long as the Cross-domain request, all not.
2, however, the Web page when you call the JS file is not affected by this
3, further promotion, we found that all have SRC attributes of the label have cross-domain capabilities, such as:<script><iframe>
4, so, if you want to access the data across the domain through the pure web-side (ActiveX control, service-side proxy, HTML5 of the future, etc.), you can only use the following methods: You try to load the data into the JS format text in the remote server, For client invocation and further processing.
5, JSON is a pure character data format, and can chant JS native support.
6. This solution comes out: The Web client invokes the dynamically generated JS format file (generally JSON suffix) on a cross-domain server in exactly the same way as the calling script.
7, the client in the JSON file after the success of the call, also got the required data, the rest is according to their own needs for processing.
8 in order to facilitate the use of data by the client, gradually formed an informal transport protocol, called JSONP. One of the key points of this protocol is to allow the user to pass a callback parameter to the server, and then the server returns the data with the callback parameter as the function name to wrap the JSON data, so that the client can customize its own function to handle the returned data.
Specific implementation:
Whether it's jquery or ExtJS or other frameworks that support JSONP, they're doing the same thing behind the scenes, and I'm going to go step-by-step and explain the implementation of JSONP at the client:
1, we know that even in the Cross-domain JS file code (of course, according to the Web Script security Policy), Web pages can also be implemented unconditionally.
The remote server remoteserver.com has a remote.js file code below the root directory:
Alert (' I am a remote file ');
The local server localserver.com has a jsonp.html page code as follows:
?
| 1 2 3 4 5 6 7 8 9 10 |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> |
There is no doubt that the page will pop up with a prompt form showing the success of Cross-domain calls.
2, now we define a function in the Jsonp.html page, and then pass in the data in the remote Remote.js to call.
The jsonp.html page code is as follows:
?
| 1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> |
The Remote.js file code is as follows:
Localhandler ({"Result": "I am remote JS bring data"});
After running to view the results, the page successfully pop-up prompt window, showing the local function is Cross-domain remote JS call success, and also received the remote JS data brought. Very happy, Cross-domain remote access to data for the purpose of basic implementation, but another problem arises, how can I let remote JS know it should call the local function called what name? After all, are JSONP service providers have to face a lot of service objects, and these service objects are their own local functions are not the same ah? We went on to look down.
3, smart developers it is easy to think, as long as the server provided the JS script is dynamically generated on the line, so that the caller can pass a parameter to tell the service side "I want a call to the XXX function of the JS code, please return to me," so the server can be in accordance with the needs of the client to generate JS script and response.
Look at the code for the jsonp.html page:
?
| 1 2 3 4 5 6 7 8 9-a |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> |
This time the code changes relatively large, no longer directly to the remote JS file to write dead, but the code to implement dynamic query, and this is the JSONP client implementation of the core, in this case the focus is on how to complete the JSONP call the whole process.
We see the URL in the call passed a code parameter, told the server I want to check the CA1998 flight information, and the callback parameter tells the server, my local callback function is called Flighthandler, so please pass the query results into this function to call.
?
| 1 2 3 4 5 |
Flighthandler ({"Code": "CA1998", "Price": 1780, "Tickets": 5}); |
We see that the pass to the Flighthandler function is a JSON that describes the basic information of the flight. Run the page, the successful pop-up prompts the window, JSONP implementation of the entire process successfully completed!
4, so far, I believe you have been able to understand the JSONP client implementation of the principle of it? The rest is how to encapsulate the code so that it is easy to interact with the user interface, enabling multiple and repeated calls.
What the? You're using jquery, wondering how jquery implements the JSONP call? Well, then I'll get the good guys and give you a piece of jquery. Using JSONP code (we still use the example of the above flight information query, assuming the return JSONP result is unchanged):
OK, the server is very smart, this page called Flightresult.aspx generated a section of this code to provide to jsonp.html (service-side implementation is not demonstrated here, and the language you choose is irrelevant, in the final analysis is the stitching string):
?
| > 1 2 3 4 5 6 7 8 9 A-and-a-list |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> |
Is it a little weird? Why didn't I write Flighthandler this time? And it worked! Haha, this is the credit of jquery, jquery when dealing with JSONP type of Ajax (still can't help spit slot, although jquery also put Jsonp into Ajax, but in fact they are not the same thing), Is it cool to automatically help you generate callback functions and take the data out for Success property method calls?
The above mentioned is the entire content of this article, I hope you can enjoy.