使用ashx解决ajax跨域访问的问题

由于跨域访问是被IE的安全访问拒绝掉的

需要使用web代理
新建一个proxy.ashx文件

在proxy.ashx里建一个webservice

代码如下:

[WebService(Namespace="http://temouri.org//")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Proxy:IHttpHandler
{
     public void ProcessRequest(HttpContext context)
     {
           string url = context.Request.QueryString["url"];
            WebRequest request = HttpWebRequest.Create(url);
           WebResponse response = request.GetResponse();
           Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);

           context.Response.ContentType = response.ContentType;
           context.Response.Write(reader.ReadToEnd());

           reader.Close();
            stream.Close();
           response.Close();
           
     }
}

调用: window.location = "proxy.ashx?url=http://www.baidu.com";

posted on 2011-04-14 16:20  一粒沙  阅读(1195)  评论(0编辑  收藏  举报