javascript - ajax 预检失败,Firefox 中的 OPTIONS 响应为 200

标签 javascript ajax firefox preflight

我有以下代码:

Util.xmlGet = function(uriend, callback) {
    var xmlhttp;
    var uri;
    if (window.XMLHttpRequest) {
        // code for modern browsers
        xmlhttp=new XMLHttpRequest();
    } else {// code for dinosaurs
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(document.URL.startsWith("file://")) {
        uri = "https://tomcat.realm:8443/worldmodel/worldmodel?";
    } else {
        uri = "/worldmodel/worldmodel?"
    }
    alert(uri+uriend);
    xmlhttp.onreadystatechange=function() {
        alert("readystate="+xmlhttp.readyState+", status="+xmlhttp.status);
        if (xmlhttp.readyState==4 && xmlhttp.status==0) {
            alert("preflight OK");
        }
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            alert("calling back");
            callback(xmlhttp.responseXML);
        }
    }
    xmlhttp.open("GET",uri+uriend,true);
    xmlhttp.setRequestHeader("Content-type","text/xml;charset=UTF-8");
    xmlhttp.send();     
   };

我在调用时看到以下警报:

  • URI
  • 就绪状态=1,状态=0
  • 就绪状态=2,状态=0
  • 就绪状态=4,状态=0
  • 预检正常

浏览器发送 OPTIONS 请求,并收到“200”响应。更准确地说:

https://tomcat.realm:8443/worldmodel/worldmodel?type=thing
Request Method:
OPTIONS
Status Code:
HTTP/1.1 200 OK

Request Headers
14:35:30.000
User-Agent:Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0
Origin:nullHost:tomcat.realm:8443
Connection:keep-alive
Access-Control-Request-Method:GET
Access-Control-Request-Headers:content-type
Accept-Language:hu,en-us;q=0.8,es;q=0.5,en;q=0.3
Accept-Encoding:gzip, deflateAccept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8


Response Headers  Δ5ms
Server:Apache-Coyote/1.1
Date:Thu, 06 Jun 2013 12:35:30 GMT
Content-Length:0
Allow:GET, HEAD, POST, TRACE, OPTIONS

永远不会发送 GET 请求,onreadystatechange 中不再有状态。

这是通过 Firefox 21.0 Mozilla Firefox for Ubuntu Canonical - 1.0(这是 help/about firefox 报告的内容)在本地文件系统中的 html 文件上完成的。

这里有什么问题吗?

最佳答案

Doesn't firefox prevent xhr request from files that are on your local filesystem to anywhere? First becuse it can't reference the same domain (local file system) for security reasons. Second because it cannot access other domains because of the same origin policy.

关于javascript - ajax 预检失败,Firefox 中的 OPTIONS 响应为 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962487/

相关文章:

javascript - office-ui-fabric/fluent-ui Grouped DetailsList

javascript - 事件监听器的范围

javascript - 使用 JavaScript 提交 Ajax.BeginForm()

javascript - 为什么 mozilla firefox 会记录来自 socket.io 1.2.1 的许多消息

javascript - Bootstrap Modal 中的 Google 方向图不起作用

javascript - IE7 中的表格行

javascript - 如何将 div 附加到带有变量 Javascript 的 div

php - cjuidialog 模式窗口上的验证

javascript - 如何在 XUL 应用程序中使用进度表?

java - 如果在单个窗口中打开多个选项卡,getWindowHandles 返回一个窗口句柄