gpt4 book ai didi

jquery - $.ajax() 在 jquery-1.6.2 中出错,但在 jquery-1.4.1 中没有

转载 作者:行者123 更新时间:2023-12-03 22:49:51 25 4
gpt4 key购买 nike

我使用 $.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 to true if a browser can create an XMLHttpRequest object and if that XMLHttpRequest object has a withCredentials property.

To enable cross-domain requests in environments that do not support cors 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com