您的位置:首页 > 职场人生

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

2016-02-15 10:29 274 查看
http://www.xuebuyuan.com/222885.html

由于跨域访问是被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";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: