【问题标题】:XMLHttpRequest not well-formed in FirefoxXMLHttpRequest 在 Firefox 中格式不正确
【发布时间】:2014-01-13 09:52:39
【问题描述】:

我有这个代码来获取 XMLHttpRequest:

var makeRequest = function () {
    var xmlhttp = getXmlHttp();
    var params = 'name=' + encodeURIComponent('123') + '&surname=' + encodeURIComponent('surname')
    xmlhttp.open("GET", 'site.html?' + params, true);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
        }
    }
    xmlhttp.send(null)
}

我有这个跨浏览器功能:

getXmlHttp = function () {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}
makeRequest()

所有代码都在本地文件中。没有服务器端。 但是 Firefox 在控制台中这样说:

'格式不正确'

那怎么了?

UPD:我添加了这个xmlhttp.overrideMimeType("text/html"); 它现在不会引发错误,但我仍然无法在 Firefox 的网络检查器中看到它 但我可以在 chrome 中看到它。

【问题讨论】:

  • xmlhttp = false; 很可能是这样。
  • 顺便说一句,在 chrome 中它可以工作

标签: javascript


【解决方案1】:

如果您指定 MIME 类型可能会有所帮助。

xmlhttp.overrideMimeType("text/html");

或者 site.html 的格式确实不正确 - 检查开始标签、结束标签等...

【讨论】:

猜你喜欢
  • 2011-07-21
  • 2010-10-15
  • 2016-04-08
  • 1970-01-01
  • 2015-05-28
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
相关资源
最近更新 更多