【.Net码农】使用ashx解决ajax跨域访问的问题

前端之家收集整理的这篇文章主要介绍了【.Net码农】使用ashx解决ajax跨域访问的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.xuebuyuan.com/222885.html


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

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

在proxy.ashx里建一个webservice

代码如下:

@H_403_16@[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";

猜你在找的Ajax相关文章