From the day of the advent of Ajax, the problem of XMLHttpRequest objects being unable to cross-domain requests has always been a classic problem, due to JavaScript's homologous strategy.
There are several ways to solve this problem:
1. How to use the middle tier transition (can be understood as "proxy")
Intermediate transition, is in the Ajax and different domains of the server communication between a layer of transition, this layer of transition can be PHP, JSP, C + + and any network communication functions of the language, from the middle tier to different domain servers to read data operations. Take an example of ASP. If you need to communicate to an ASP. XMLHttpRequest of a different domain, now the client is going to query one of the domain's ASP. This ASP. NET from this domain is then communicated to the different domains of ASP. NET response Output (response) by this domain.
2. Using the
This method uses src from the
Page code:
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <title>Ajax cross-domain issues</title> <Scripttype= "Text/javascript"src=""ID= "Getaspx"> </Script> <Scripttype= "Text/javascript"> functionget (URL) {varobj=document.getElementById ("getaspx"); OBJ.SRC=URL; (Obj.readstatus== $) {alert (responseval);//If successful, it pops up Dylan } } functionquery () {get (getdemo.aspx); } </Script></Head><Body><inputtype= "button"value= "Ajax cross-domain test"onclick= "query ();"/></Body></HTML>
View Code
Getdemo.aspx Background code:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;namespace learnjs{public partial class GetDemo:System.Web.UI.Page { protected void Page_Load (object sender, EventArgs e) { Response.Write ("var responseval= ' Dylan '"); } }}
View Code
This method, also called Ajaj or Ajax without XMLHttpRequest, converts x to J because it uses
3. jquery Solution
Page code:
<HTML><Head><title>jquery Learning</title><Scriptsrc= "Jquery-1.4.2.min.js"type= "Text/javascript"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){ varobtntest= $("#btnTest"); Obtntest.click (function() {obtntest.disabled= true; varOresult= $("#result"); Oresult.html ("Loading"). CSS ("Color","Red"); Jquery.getscript ("Http://www.test.com/test/js.txt", function() {oresult.html ("Name:" +Dylan.name+ "<br/>email:" +dylan.email). CSS ("Color","Black"); Obtntest.disabled= false; }); }); }); </Script></Head><Body><ButtonID= "Btntest">Btntest</Button><DivID= "Result"></Div></Body></HTML>
View Code
The contents of the remote server-side Js.txt are:
var dylan= {name: "Dylan", Email:[email protected]}
Ajax Cross-Domain solutions