【问题标题】:PUT/DELETE XMLHttpRequest Not Working in FirefoxPUT/DELETE XMLHttpRequest 在 Firefox 中不起作用
【发布时间】:2013-08-14 21:57:08
【问题描述】:

我正在处理 javascript 跨域 ajax 请求。我的代码在 chrome 和其他设备上运行良好,例如 android 浏览器和使用 phonegap 的 android 本机应用程序。 但我遇到了 Firefox 的问题..

Firefox 不支持我的 PUT 和 DELETE 请求。 Firefox 有什么解决方案可以向我的服务器发出 put 和 delete 请求。

firefox 确实支持我的帖子和获取请求。两者都要求工作正常。

这是我的工作代码。

    var XMLHttpFactories = [
function () {
    return new XMLHttpRequest()
},
function () {
    return new ActiveXObject("Msxml2.XMLHTTP")
},
function () {
    return new ActiveXObject("Msxml3.XMLHTTP")
},
function () {
    return new ActiveXObject("Microsoft.XMLHTTP")
}
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
        try {
            xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}

用于发送 Put 请求..

var xhr = createXMLHTTPObject();
xhr.open("PUT", url,true);

    xhr.onreadystatechange=function()
    {
        if (xhr.readyState==4)
        {
            if(xhr.status==200){
                request.success(xhr.responseText);
            }else if(xhr.status!=200){
                request.error(xhr.responseText)
            }
        }

    }
    xhr.send(body);

【问题讨论】:

  • 您应该发布执行PUTDELETE 请求的代码,而不是发布工厂代码
  • 有任何来自 FireFox 的错误消息吗?
  • 在 Firefox 中没有发生任何事情......没有收到任何错误......
  • 我觉得没问题:jsfiddle.net/C275E/1

标签: javascript ajax html xmlhttprequest


【解决方案1】:

以下在 Firefox 22.0(和 23.0 也一样)上运行良好:

var XMLHttpFactories = [
function () {
    return new XMLHttpRequest()
},
function () {
    return new ActiveXObject("Msxml2.XMLHTTP")
},
function () {
    return new ActiveXObject("Msxml3.XMLHTTP")
},
function () {
    return new ActiveXObject("Microsoft.XMLHTTP")
}
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
        try {
            xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}

var xhr = createXMLHTTPObject();
xhr.open("PUT", "/echo/html/", true);
xhr.onreadystatechange = function()
{
    if (xhr.readyState == 4)
        alert("Request completed, with the following status code: " + xhr.status);
}
xhr.send("");

这是一个 jsFiddle:http://jsfiddle.net/qXQtD/

为了更好地了解您的情况,请回答以下问题:

  1. 您要发送的数据是什么?
  2. 您的完整响应标头是什么(尤其是“Access-Control-Allow-Origin”标头)?

【讨论】:

    猜你喜欢
    • 2017-09-14
    • 1970-01-01
    • 2015-02-08
    • 2011-03-23
    • 2017-07-17
    • 1970-01-01
    • 2012-03-16
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多