Ajax跨域访问

1  function GetExamDynamicInfo() {
2                 var url = "http://localhost:9966/exam.cbi360.com/ExamDynamic.ashx?op=Exam&spid=" + sid + "&top=5&t=" + new Date().getTime();
3                 _examrequest.open("Get", url, true);
4                 _examrequest.onreadystatechange = ShowExam;
5                 _examrequest.send(null);
6         }
7  

 

  通过使用ajax可以使用异步操作,做到页面局部无刷新的效果。在本地两个项目之间如果说是需要在A项目中通过ajax访问B项目,url传入的是B项目中的要访问的地址,那么在IE中的左下角会有错误警告,提示没有权限访问。如果是在谷歌中则无提示,但获取不到。怎么回事呢?原来js是无法进行跨域访问的,既然用js无法访问,那就用web代理的方式访问吧。

 

 1   function GetExamDynamicInfo() {
 2                 var url = 
 3  send_screquest("Text?url=" + encodeURIComponent("http://localhost:9966/exam.cbi360.com/Handler/ExamDynamic.ashx?op=getExam&spid=" + sid + "&top=5&t=" + new Date().getTime();
 4 );    
 5 //将此url改为当前项目中的代理页面:Text.aspx
 6                 _examrequest.open("Get", url, true);
 7 
 8                 _examrequest.onreadystatechange = ShowExam;
 9                 _examrequest.send(null);
10         }

  通过Text.aspx获取即可,只在.cs页面中写入以下代码即可,可以将页面的信息删掉,留下最上边的一行<%...%>即可

1  string url = HttpUtility.UrlDecode(Request.QueryString["url"]);
2         WebClient myWebClient = new WebClient();
3         byte[] myDataBuffer = myWebClient.DownloadData(url);
4         string data = Encoding.UTF8.GetString(myDataBuffer);
5         Response.Write(data);
6         //Response.End(); 

 

转载于:https://www.cnblogs.com/lollipop/archive/2012/08/07/2627375.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值