1
function loaded() {
    var xmldoc,
    currenttime = new Date().getTime(),
    req,
    address = 'http://webservices.foo.com/eSignalQuotes/eSignalQuotes.asmx/GetDelayedQuotes?',
    symbols = 'symbols=' + '+c,s,ct,zw,kw,adm+', 
    cusip = '&cusip=',
    fields = '&fields=' + 'desc,month,year,recent,netchg,-decimal',
    type = '&type=' + 'future,stock,index',
    dispfullname = '&dispfullname=' + 'true',
    datefmt = '&datefmt=',
    timefmt = '&timefmt=',
    timestamp = '&' + Math.floor(currenttime/3600000),
    query = address + symbols + cusip + fields + type + dispfullname + datefmt + timefmt + timestamp;
    ;

    if(window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    req.addEventListener('error', function(e) {alert('Error');}, false);
    req.addEventListener('load', function(e) {xmldoc = req.responseText;}, false);
    req.open('GET', query, true);
    req.send();

}

这就是我的代码的样子,它总是在 Safari 和 Firefox 中引发错误。疯狂的是,如果我删除事件侦听器,并将响应类型更改为responseText,Internet Explorer 会给我输出。我试过overrideMimetype了,但这似乎没有帮助。如果我在 Firefox 或 Safari 中检查响应,我会得到null. 我很茫然,任何帮助将不胜感激。

我应该提到,我宁愿为此避免任何 3rd 方库。

更新:错误发生在progress事件期间,如果我检查.lengthComputable我得到false

更新 2:Safari 更清楚地说明了这个问题:

XMLHttpRequest cannot load Origin is not allowed by Access-Control-Allow-Origin.
4

1 回答 1

0

我不能 100% 确定,但在我看来,这个问题涉及跨站点通信。我最终做的是让 PHP 脚本下载文件,然后我使用 javascript 在本地获取它。

<?php
    $mark = $_GET['mark'];
    $xmldoc = new DOMDocument();
    $xmldoc -> preserveWhiteSpace = false;
    $xmldoc -> formatOutput = true;
    $xmldoc -> load($mark);
    unlink('fenced.xml');   
    echo $xmldoc -> save('fenced.xml');
?>

Javascript:

localreq.open('GET', 'fenced.xml', true);
localreq.addEventListener('load', function(e) {xmldoc = localreq.responseXML;}, false);
localreq.send();
于 2012-10-08T19:37:33.890 回答