【问题标题】:XmlHttpRequest status = 0 on remote server, status 200 on localhost远程服务器上的 XmlHttpRequest 状态 = 0,本地主机上的状态 200
【发布时间】:2013-04-22 17:14:18
【问题描述】:

我有两个几乎相同的 PHP 代理脚本,我通过几乎相同的 JS 调用它们。第一个在所有情况下都可以正常工作。

第二个(下面的代码)在 Firefox 和 Chrome 中的本地 IIS 服务器上工作。在远程 Glassfish Apache 服务器上,它在 Firefox 中工作,但在 Chrome 中返回 status = 0 并返回空(在 v22 和 v26 中测试)。这两个 PHP 代理脚本实际上是相同的,并且下载的数据长度相似(在 1 到 4 MB 之间)。使用下面的 JS,else 和 onerror 警报都会被触发。

var dateQueryText = interviewDays[exitDay];
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4){
        if (xmlhttp.status == 200){
            var xmlDoc=xmlhttp.responseXML;
            processGPX(xmlDoc);
        }
        else{alert("Download of GPS data failed for date " + dateQueryText + " Status: " + xmlhttp.status);} 
    }
}
xmlhttp.onerror = function (e){alert("Error encountered and download of GPS data failed for date " + dateQueryText + " Status: " + xmlhttp.status);};

xmlhttp.open("POST","proxy2.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
params = ("case=" + caseID + "&gps=true" + "&date=" + dateQueryText);
xmlhttp.send(params);

我尝试了以下方法无济于事:

  • async & synchronous 下载
  • 覆盖return 类型,因为服务器{xmlhttp.overrideMimeType("text/xml");} 并不总是能够理解GPX(XML 格式的GPS 数据)。
  • 提供 PHP 脚本的完整路径

什么可能导致 Chrome 出现问题?

编辑:这是失败的 PHP 请求的请求标头。没有收到回复。

Request URL: https://www.XXX.edu/AAA/BBB/CCC/proxy2.php

Request Headers
Cache-Control:no-cache
Content-type:application/x-www-form-urlencoded
Origin:https://www.XXX.edu
Pragma:no-cache
Referer:https://www.XXX.edu/AAA/BBB/CCC/exitgraphic.php?case=TESTCASE&instrument=0001&mode=TEST
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Ubuntu/12.10 Chromium/22.0.1229.94 Chrome/22.0.1229.94 Safari/537.4

Form Data
case:TESTCASE
inst:0001
mode:TEST
gps:true
date:02222013

【问题讨论】:

    标签: php javascript ajax apache xmlhttprequest


    【解决方案1】:

    您能否在激活 Chrome 的开发者工具(工具 --> 开发者工具)的情况下重试失败的请求,并在此处发布请求和响应的标头(在“网络”选项卡中)以及“控制台”选项卡中的任何错误消息?

    【讨论】:

    • 在上面添加了请求标头。没有收到任何回复。有了上面的JS,我无法弄清楚实际的错误是什么,因为xmlhttp.onerror中的e对象是XMLHttpRequestProgressEvent类型的。
    • 当我将其切换到同步模式时,我得到“NETWORK_ERR: XMLHttpRequest Exception 101”堆栈:“错误:同步请求中发生网络错误。
    • 这是跨域请求吗?如果是这样,您可能需要为您的服务器添加对CORS 的支持。
    • 同一个域。代理脚本与请求 URL 位于同一目录中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多