2014-04-03 173 views
0

从我的html页面调用MVC webservice时,它总是出错;请帮助我跨域Ajax调用

$.ajax({ 
    type: "GET", 
    url: url, 
    contentType: "application/json; charset=utf-8", 
    crossDomain: true, 
    dataType: "json", 
    success: function (msg) { 
     alert(Response.ErrorMessage) 
    }, 
    error: function (e, status) { 
     alert(e + " Fail " + status) 
    } 
}); 
+1

你试过'dataType:“jsonp”'? 你得到哪个错误信息? – MaiKaY

+0

你不需要'crossDomain:true,'或'contentType:“application/json; charset = utf-8”,' –

+0

首先打开** javascript调试控制台**并寻找错误消息。接下来,仍然在您的** javascript调试控制台**中,查看发生的网络请求,以查看您的服务器是否返回了您的预期结果。 –

回答

2

您必须设置CORS(http://www.html5rocks.com/en/tutorials/cors/)或使用JSONP。如果您决定使用JSONP,则必须使用JSON数据发送适当的回调函数名称。

+1

为了补充这个答案:如果你想使用JSONP,也请看看可能的[安全问题](https://en.wikipedia.org/wiki/JSONP#Security_concerns)它... –

+0

是的,你应该使用CORS,如果它对你来说是可行的选择。 – rasmusx