Cross-domain Ajax request
In the past couple of days I do a small project, and that small project in the distribution of the above js code for customer use.Js code that will eventually show html complete documents, and there will be interactive Ajax.As the Ajax can not cross-domain, I find some solutions from Google, but do not feel good.
For example, add Iframe and mandatory set document.domain, had use of proxy documents.
Later, a friend analysis of cross-border certification ajax show that this approach is feasible.The principle is to add a dynamic section script to the head of the page , the script will be definite a js variable, the page will be able to call the js variable.the code in xxx.xxx may be as follows:
var v = ‘variable here is that you’;
Js core code, create a dynamic script to the page head
- function request(page, pagesize, appid)
- {
- var url = ‘xxx.xxx’;
- oScript = Find(‘RemoteScript’);
- var head = document.getElementsByTagName(“head”).item(0);
- if (oScript) {
- head.removeChild(oScript);
- }
- oScript = document.createElement(“script”);
- oScript.setAttribute(“src”, url);
- oScript.setAttribute(“id”,‘RemoteScript’);
- oScript.setAttribute(“type”,“text/javascript”);
- head.appendChild(oScript);
- return oScript;
- }
in the any region of the pages will be able to use the v variable.Although this is not a very good solution, but also feeling good, there is a fault is that can not use the variable v until the script fully loaded, of course, it can be solved.Otherwise, you also can directly used getJson in jQuery which support the cross-border to deal with json document.
Leave a Reply
You must be logged in to post a comment.