jquery-1.6.2中的$ .ajax()错误,但jquery-1.4.1中没有

时间:2011-07-26 10:45:41

标签: jquery ajax web-services

我使用$.ajax()来使用本地.asmx webservice。这是我的电话代码:

    $("#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时,我得到一个errorThrownNo Transport

还有什么我想念的吗?

1 个答案:

答案 0 :(得分:7)

当您尝试使用Ajax时,您的HTML + JS页面可能未从localhost:53003加载,该网址位于域localhost:53003上。

因此,看起来您正在尝试制作跨域请求,这是禁止的 - 请参阅Same Origin Policy


查看jQuery.support的文档,您可能需要启用jQuery.support.cors (引用)

  如果浏览器可以创建cors,则

true等于XMLHttpRequest   对象以及XMLHttpRequest个对象是否有withCredentials   属性。

在环境中启用跨域请求   但是不支持cors但允许跨域XHR请求   (Windows小工具等),设置$.support.cors = true;


以下是一些可能有用的链接: