Recently, I encountered cross-domain ajax access. Finally, I used the Application Proxies method to solve the problem, that is, place a proxy file in the domain (depending on the development languages supported by the domain, select asp, asp.net, or others). This proxy file sets the url parameter (QueryString) go to the corresponding page of the target domain to get the html code and output it. Ajax directly accesses this proxy file for cross-origin purposes.
The asp.net-based cross-origin access proxy file c # code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" ResponseEncoding = "UTF-8" %>
<% @ Import Namespace = System. Net %>
<% @ Import Namespace = System. IO %>
<Script runat = "server">
Protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e );
String sourceUrl = "http://devspy.net ";
This. Page. Response. Write (TransferHtmlPage (string. Concat (sourceUrl ,"? ", This. Page. Request. QueryString )));
}
Public string TransferHtmlPage (string url)
{
String result = string. Empty;
Try
{
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (url );
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding. UTF8 );
Result = reader. ReadToEnd ();
}
Catch (Exception ex)
{
Return string. format (@ "<p style = 'color: red; text-align: center; '> An error occurred while obtaining the file content on the server: {0} </p>", ex. message );
}
If (! CheckVersionWaterMark (result ))
Return @ "<p style = 'color: red; text-align: center; '> the version watermark is invalid. Contact the related technical staff. </P> ";
Return result;
}
Public bool CheckVersionWaterMark (string inputString)
{
Return true; // the watermark is not verified.
// String pattern = "WaterMark ";
// Return Regex. IsMatch (inputString, pattern, RegexOptions. IgnoreCase );
}
</Script>
In addition, the Implementation Based on asp and php is not listed. If you are interested, you can download a compressed package containing these three files:
Http://files.cnblogs.com/cncxz/ajaxProxy.rar