如何解决AJAX跨域的问题?(跨域Cookie读取)
之前发了一个贴,http://topic.csdn.net/u/20091214/09/2ddb3ead-2891-4c6d-93bf-ae2110b4b109.html
我要做的是一个跨域的读取Cookie的操作,www.bbb.com\test2.aspx调用www.aaa.com\的test1.aspx返回www.aaa.com的Cookie.
本是想用GetResponse()来做,但后来发现这样取得的Cookie是null.
所以只能在页面用AJAX的方式来调用。但写完后出现了AJAX跨域无法访问的问题。
希望大家帮助解决一下。代码如下:
www.aaa.com\test1.aspx.cs:
protected void Page_Load(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies["hello"]; if ((cookie != null)) { Label1.Text = cookie.Value; TextBox1.Text = cookie.Value; //Response.ContentType = "text/HTML"; Response.Write(cookie.Value); Response.End(); } else { Response.Write("no Cookie"); Response.End(); } } <script language="JavaScript"> var xmlHttp ; function createXMLHttpRequest() { if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function doPostBack() { var url = "http://www.aaa.com/test1.aspx"; createXMLHttpRequest() ; xmlHttp.open("GET",url,true); xmlHttp.onReadyStatechange = showCallBack; xmlHttp.send(null); } //回调函数 function showCallBack() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { success(); } } } //解析xml function success() { var responseXml; responseXml = xmlHttp.responseXML; var text= xmlHttp.responseText; if(text!=null&&text.length>0) { alert(text); } else { info = xmlHttp.responseText; alert(info); } } </script>
Response.AddHeader("P3P", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"); Response.Cookies["hello"].Path = "/"; Response.Cookies["hello"].Domain = ".test2.com"; Response.Cookies["hello"].Expires = DateTime.Now.AddDays(10); Response.Cookies["hello"].Value = UrlFrom.ToString();
[解决办法]
楼上的.都说了能写不能读了. 楼主要的是跨越读Cookie.
[解决办法]
xuexi
[解决办法]
楼主试试~~
http://blog.csdn.net/sweetsoft/archive/2006/02/05/592408.aspx
那个仙女散花
[解决办法]
用jquery的getJSON能解决跨越ajax,我以前做过的.可以.
<script type="text/javascript" src="Jscript/jquery-1.3.2.min.js"></script> <script type="text/javascript"> function ss() { $.getJSON("http://www.aaa.com/test1.aspx?jsoncallback=?",function(json){ alert(json.ID); }); } </script>
[解决办法]
js写错了.改下.
$.getJSON("http://www.aaa.com/test1.aspx?jsoncallback=?",function(json){alert(json.msg);
[解决办法]