Cross-origin JavaScript implementation

Source: Internet
Author: User
Tags subdomain

Cross-origin JavaScript implementation
Recently I am working on a file upload service, which contains an uploaded web page. The purpose is to nest the web page on the client page and then directly upload the file to the server. Because the files are different, the names of the folders to be saved are different. Therefore, the client needs to pass a folder name to inform the server of the target location to be saved. This involves passing the iframe parameter: after the Client file is uploaded successfully, the Server returns the file name or related information to the Client for other operations. This involves another parameter transfer: Server> Client is the overview of this upload tool. However, my notes are mainly about parameter passing. Here I have covered the JavaScript cross-origin issue. My implementation is divided into two situations. There are also two ways to implement (there are many cross-origin instances and instructions on the internet). The first situation is: the primary domain is the same and the subdomain is different. For example, a.baidu.com and B .baidu.com use document. domain = "ceshi.com" Statement, which is added to both the client and server pages. Copy code 1 document. domain = "ceshi.com"; // The primary domain is the same, and the subdomain is different. 2 $ ("# mainFrame "). load (function () {3 var clientHeight = $ ("# mainFrame "). contents (). find ("body "). height (); 4 var clientWidth = $ ("# mainFrame "). contents (). find ("body "). width (); 5 $ (this ). height (clientHeight); 6 $ (this ). width (clientWidth); 7 $ ("# mainFrame "). contents (). find ("input [id = hiddenId]"). val ("100"); 8 // if the primary domain is the same and the subdomain is not the same, an id9 parameter is passed); if the primary domain is different, the primary domain is the domain. Different names, such as www.baidu.com and www.sohu.com, are troublesome. I use window. location. hash, and a proxy page is required on the client. Transfer principle: when calling the client homepage, use # currentFolder #### PASS Parameters. On the Server Page, use the following code to receive and save the code to an input den. Copy the Code 1 // The following Code indicates that the primary domain is not at the same time. 2 var data = {}; 3 var hash_url; 4 function dealHash () {5 hash_url = window. location. hash; // get parameter 6 var datas = hash_url.split ("#") [1]. split ("&"); 7 for (var I = 0; I <datas. length; I ++) {8 var t = datas [I]. split ("="); 9 data [t [0] = decodeURIComponent (t [1]); 10} 11} 12 13 function change () {14 if (hash_ur L! = Window. location. hash) {15 dealHash (); 16 document. getElementById ("FolderName "). value = data ["currentFolder"]; 17} 18} copy the code and call the callback method to pass the file name after the upload is successful. This is exactly the opposite of passing parameters from the client to the server, however, this information is sent to the client proxy. In this way, it is easier for the Agent Page of the client to communicate with the home page. 1 function callback (data) {2 var iframe = document. getElementById ("mainFrame"); 3 var url = "http://t.ceshi2.com/proxy.html? "+ New Date (). getTime (); 4 iframe. src = (url +" # filename = "+ data); 5}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.