6种JS跨域方式,只有第5个在阿帕奇下好使

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=uft-8" />
<script src="jquery.js" type="text/javascript"></script>  
<script language="javascript" type="text/javascript"> 
    function Search1() { 
   $.post('http://####/123.svc/222222/UserId=@@@',{}, function (data,statu) {
               alert(data);
      if (statu == 'success') {
     alert(data);
                 }
             });
    } 
    function Search2() { 
   $.post('http://#####/222222.ashx', { "rrrrrr":'@@@' }, function (data,statu) {
                 if (statu == 'success') {
     alert(data);
                    if(data=="false"){
                       alert("请登录后再进行此操作");
                    }
                    else{
                     alert(123);
                    }
                 }
             })
    } 
 
    function Search3() { 
  $.ajax({
            async:false,
            url: 'http://####/123.svc/22222/UserId=@@@',  // 跨域URL
            type: 'GET',
            dataType: 'jsonp',
            jsonp: 'jsoncallback', //默认callback
            timeout: 5000,
            beforeSend: function(){  //jsonp 方式此方法不被触发。原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
            },
            success: function (json) { //客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
                if(json.actionErrors.length!=0){
                    alert(json.actionErrors);
                }
              //  genDynamicContent(qsData,type,json);
  alert(json[0].value);
            },
            error: function(xhr){
                //jsonp 方式此方法不被触发
                //请求出错处理
                alert("请求出错(请检查相关度网络状况.)");
            }
        });
    } 
 
 
 function  Search4()
 {var result = "";
 var xhr = new XMLHttpRequest();
xhr.open("get", 'http://####/2222.ashx?rrrrr=@@@', true);
xhr.setRequestHeader("If-Modified-Since", "0");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
 result = xhr.responseText;
alert(result);
}
};
xhr.send(null);
 }
 
function  Search5()
 {
$.ajax({
type : "get",
url  : "http://####/22222.ashx?rrrrr=@@@",
dataType : "jsonp",
jsonp: "jsoncallback",        
success : function(msg) {
if(msg){
alert(msg[0].value);
}
}
});
}
 function  Search6()
 {
 $.getJSON("http://####/22222.ashx?rrrrr=@@@", {}, function(response)
 {
 debugger;
                 alert(response);
  });
}
</script>
</head>
<body>
    <form id="Form1" method="post" runat="server">
  <input type=button value="1" οnclick=Search1()>
  <input type=button value="2" οnclick=Search2()>
  <input type=button value="3" οnclick=Search3()>
  <input type=button value="4" οnclick=Search4()>
  <input type=button value="5" οnclick=Search5()>  <input type=button value="6" οnclick=Search6()>
  <div id="msg">

    </div>
    </form>
</body>
</html>

 

 

==========================后台只能这样搞,WCF的貌似不好使

 

 

   if (context.Request["jsoncallback"] != null)
            {
                string jsoncall = context.Request["jsoncallback"].ToString();
                //封装JSon格式 
                sb.Append(jsoncall + "([{\"value\":\"");
                sb.Append(pk);
                sb.Append("\"}])");
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值