当前位置: 代码迷 >> Web Service >> ajax跨域调用c# webserver 回到json数据的几点疑惑
  详细解决方案

ajax跨域调用c# webserver 回到json数据的几点疑惑

热度:820   发布时间:2013-06-19 10:26:41.0
ajax跨域调用c# webserver 返回json数据的几点疑惑
站点A    c# webserver  代码


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json,XmlSerializeString = false)]
        public void Login(string userName, string userPwd, string macAddress)
        {
            var response = HttpContext.Current.Response;
            response.ContentType = "text/json";  

            var ws = ValitateLogin(userName, userPwd, macAddress);
            var json = new JSONHelper {success = true};
            json.AddItem("UnReadEmailNum", ws.UnReadEmailNum.ToString());
            json.AddItem("IsValid", ws.IsValid.ToString());
            json.AddItem("RetString", ws.RetString);
            json.AddItem("EmailUrl",ws.EmailUrl);
            json.ItemOk();
            response.Write(json.ToString());  

        }


 站点B  ajax 跨域调用代码
  $(function () {
            $("#jsonp_btn").click(function () {
                
        $.ajax({    
             async:false,    
             url: 'http://192.168.1.2:14671/OA/Email.asmx/Login',  // 跨域URL   
             type: 'GET',    
             dataType: 'jsonp',   
            contentType: "application/json; charset=utf-8", 
            jsonp: 'jsoncallback', //默认callback  
            data: {"userName":"admin","userPwd":"123456","macAddress":""}, //请求数据   
            timeout: 5000,    
            beforeSend: function(){  
                        //jsonp 方式此方法不被触发。原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了   
                    },   
                    success: function(json) { 
  相关解决方案