【问题标题】:Problems when calling $.Ajax in Firefox and Chrome in a cross domain call在跨域调用中在 Firefox 和 Chrome 中调用 $.Ajax 时出现问题
【发布时间】:2012-04-06 00:09:29
【问题描述】:

我在 url 中有一些 asmx Web 服务(即 example.com/Entities/WebServices 假设 example.com 是我的域)。这样做的目的是能够从所有应用程序中访问像员工一样的域数据。因此,对于获取员工数据的每个示例,我使用方法example.com/Entities/WebServices/EmployeeWs.asmx/GetAll

当我尝试使用 JQuery.Ajax 从本地主机获取员工数据时,它在 IE8 上运行良好。但是在使用 Firefox 时,$.ajax 调用将我发送到成功函数但带有一条空消息(还检查了 firebug 的响应,它是 200 OK - 0byte)

我已经把 jQuery.support.cors = true;但它在 Firefox 或 chrome 中没有帮助。

我也尝试在example.com/TestProject 中发布测试应用,但结果是一样的。它只适用于 IE。

有人知道错误的原因吗?

使用 IE8、Firefox 8.0.1、Chrome 11.0.6。 jQuery 1.7.1

提前致谢。

var requestedUrl = "http://example.com/Entities/WebServices/EmployeeWs.asmx/GetAll";

jQuery.support.cors = true;

$.ajax({
    type: "POST",
    contentType: "application/json",
    url: requestedUrl,
    dataType: 'json',
    contentType: "application/json; charset=utf-8",

    success:

    function(result) {

        alert("ok. " + result);

    },

    error:

    function(xhr) {

        alert("err. " + xhr.responseText);

    }

});

【问题讨论】:

    标签: jquery asp.net ajax firefox google-chrome


    【解决方案1】:

    端口/协议是否相同?例如,如果您从 https://example.com 调用 http://example.com,就会出现此错误。

    编辑:看看那里。我自己没有测试。 How do jQuery do its cross-domain ajax calls and how can I replicate em with mootools

    【讨论】:

    • 是的,我使用的是 http 协议,但每个 Web 应用程序都驻留在同一服务器中的特定端口和应用程序中。如果我不使用 jQurey.support.cors=true 语句,我也会在 IE 中收到跨域错误。但是把它放在资源管理器中,我在 Firefox 中没有错误,但也没有返回数据。
    • 我认为你的问题来自那个时候。如果每个应用程序都在不同的端口上,则将其视为另一个域。
    • 是的,我知道,但是如果我使用 jQuery.support.cors = true,它应该可以工作,不是吗?在 IE 中它也在工作,我在任何浏览器中都没有收到错误,只是没有接收到数据。
    • CORS 的全部意义在于允许跨域请求,因此是他最初的问题。
    猜你喜欢
    • 2011-02-10
    • 2010-09-23
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多