我使用 $.ajax()
来使用本地 .asmx Web 服务。这是我的调用代码:
$("#btnGetOne").click(function() {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'http://localhost:53003/TestWebService.asmx/GetServant',
data: '{ "servant_id": "' + $("#txtServantID").val() + '" }',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
var jsnData = $.parseJSON(data.d);
$('#DisplayArea').html(jsnData.Servant_Name);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + ' ' + errorThrown);
}
});
});
如您所见,当我单击 btnGetOne 时,ajax 调用就会执行。
正如我的问题标题所示,这在 jquery-1.4.1 中有效,但是当我使用 jquery-1.6.2 时,我收到一个 errorThrown
提示 No Transport
。
我还有什么遗漏的吗?
最佳答案
当您尝试使用 Ajax 域 localhost:53003
上的 URL 时,您的 HTML+JS 页面可能未从 localhost:53003
加载。
因此,您似乎正在尝试发出跨域请求,这是被禁止的 - 请参阅 Same Origin Policy .
查看 jQuery.support
的文档,您可能需要启用 jQuery.support.cors
(引用) :
cors
is equal totrue
if a browser can create anXMLHttpRequest
object and if thatXMLHttpRequest
object has awithCredentials
property.
To enable cross-domain requests in environments that do not supportcors
yet but do allow cross-domain XHR requests (windows gadget, etc), set$.support.cors = true;
.
这里有一些可能有帮助的链接:
关于jquery - $.ajax() 在 jquery-1.6.2 中出错,但在 jquery-1.4.1 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6828852/