爱东东

VS.NET,IT,个人,个人网站 爱东东 http://www.idongdong.net

 

JS跨域获取url返回例子

A域(a.com)中server.aspx.cs的Page_Load事件中,获取传递入的参数,经过业务逻辑,最终通过Response.Write回写如下:
this.Response.Write("var g_result=\""+s+"\";");

B域(b.com)中client.aspx页面代码中调用如下:
<script language=javascript>
  function f_getresultfromjs(url,name,func){
   this.timeid=-1;
   this.f_calljs=function(){
    var script = document.createElement('script');
    script.setAttribute( "src" , url);
    script.type = 'text/javascript';
    document.body.appendChild(script)
   }
   this.f_getresult=function(){
    if(this.timeid!=-1){
     window.clearTimeout(this.timeid);
    }
    try{
     var obj=eval(name);
     if(typeof(obj)=="undefined"){
      this.timeid=window.setTimeout("this.f_getresult();",1000);
     }else{
      func(obj);
     }
    }catch(e){
     this.timeid=window.setTimeout("this.f_getresult();",1000);
    }
   }
   this.f_calljs();
   this.f_getresult();
  }
  function f_getfromjs(httpresult){
   //对返回的httpresult进行处理
  }
  function f_dojs(){
   var callurl = "http://www.a.com/server.aspx?aid=1&bid=1";
   f_getresultfromjs(callurl,"g_result",f_getfromjs);
  }
</script>

在页面写一个button,onclick事件中写入调用方法
<input type=button onclick="f_dojs();" value="Test" />

posted on 2007-04-13 10:00  爱东东  阅读(1913)  评论(0编辑  收藏  举报

导航