How to overcome cross-domain ajax call:
1, Use Proxy
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="../JS/Framework.js"></script>
<script type="text/javascript" >
function test()
{
var div = document.getElementById("status");
var request = MSXMLUtil.createXMLRequest();
var url = "Proxy.aspx?id=1&name=xiaolin";
request.open("POST", url, true);
request.onreadystatechange=handleChange(request, div);
request.setRequestHeader("Content-Type", "application/x-www.form-urlencoded");
request.send(null);
div.innerHTML="Loading
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
}
function handleChange(request, div)
{
return function()
{
if(request.readyState==4)
{
if(request.status==200)
{
div.innerHTML=request.responseText;
}
}
}
}
</script>
</head>
<body>
<input type="button" value="Cross-domain Ajax Call" onclick="test()" />
<div id="status" style="background-color:Blue; color:White"></div>
</body>
</html>
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class AjaxProxy
{
public static string TransferAjaxCall(string postData)
{
HttpWebRequest request = WebRequest.Create("http://abc.com/test/handle.aspx") as HttpWebRequest;
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(postData);
if (request != null)
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//Detect the proxy.
request.Proxy = WebProxy.GetDefaultProxy();
Stream postStream = request.GetRequestStream();
postStream.Write(buffer, 0, buffer.Length);
postStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
Encoding resEncoding = Encoding.GetEncoding("UTF-8");
StreamReader reader = new StreamReader(webResponse.GetResponseStream(), resEncoding);
string result = reader.ReadToEnd();
reader.Close();
webResponse.Close();
return result;
}
else
{
return string.Empty;
}
}
}
public partial class CrossDomainAjax_Proxy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string postData = "id=" + 1 + "&name=" + "xiaolin";
string ret = AjaxProxy.TransferAjaxCall(postData);
ViewState["Ret"] = ret;
}
protected override void Render(HtmlTextWriter writer)
{
if (ViewState["Ret"] != null)
{
writer.Write(ViewState["Ret"].ToString());
}
}
}
2, Use src property of <script></script> object to call cross-domain server.
3, Still thinking...