使用jsonp从其他域调用rest Web服务的Ajax Call

时间:2012-12-10 14:12:33

标签: html

当我从不同的域调用rest webservice时我如何使用jsonp.bec我的简单json不用于休息webservice.how我可以调用rest webservice for ajax call.i同时使用jsonp和javascript for.server responce i仅在firebug中的浏览器上看到,但如何在我的应用程序中显示它

我的JS代码。

function callService(){             $就({                 type:varType,// GET或POST或PUT或DELETE动词                 url:varUrl,//服务的位置                 data:varData,//发送到服务器的数据                 crossDomain:true,                 cache:false,                 async:false,                 contentType:varContentType,//发送到服务器的内容类型                 dataType:varDataType,//来自服务器的预期数据格式                 processdata:varProcessData,// True或False                 success:function(msg){//在成功的服务调用上                     alert(“Server Responce Successfully!..”);                 },                 错误:function(msg){                     alert('error'+ msg.d);                 }             });         }         function countryProvinceWCFJSONMulti(){

        var head = document.getElementsByTagName('head')[0];
        script = document.createElement('script');
        script.type = 'JSON';
        script.src = "http://192.168.15.213/myservice/Service.svc/GetEvents";
        script.type = "text/javascript";
        head.appendChild(script);
        varType = "GET";           
        varData = '{"username": "' + "Suhasusername" + '","password": "' + "suhaspassword" + '"}';
        varContentType = "application/JSONP; charset=utf-8";
        varDataType = "JSONP";
        varProcessData = true;
        callService();
    } 

1 个答案:

答案 0 :(得分:0)

如果同源策略不是问题,那么您的代码应如下所示:

$.ajax({ url: url,
         dataType: 'jsonp',
         contentType: 'application/json',
         success: function (data) {
               alert("Server Response Successful!..");
        }
    });

Here is a question谈论绕过同源政策