Ajax请求总是引发错误

时间:2022-10-07 15:00:50
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();

}

That's what my code looks like, and it always throws an error in Safari and Firefox. The crazy thing is if I remove the event listeners, and change the response type to responseText, Internet Explorer gives me output. I tried overrideMimetype, but that didn't seem to help. If I check the response in Firefox or Safari, I get null. I'm at a loss, and any help would be appreciated.

这就是我的代码看起来像,它总是在Safari和Firefox中引发错误。疯狂的是,如果我删除事件侦听器,并将响应类型更改为responseText,Internet Explorer将为我提供输出。我尝试了overrideMimetype,但这似乎没有帮助。如果我在Firefox或Safari中检查响应,我会得到null。我很茫然,任何帮助都会受到赞赏。

I should mention that I'd prefer to avoid any 3rd party libraries for this.

我应该提一下,我宁愿避免使用任何第三方库。

Update: The error occurs during the progress event, and if I check .lengthComputable I get false

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

Update 2: Safari sheds more light on the issue:

更新2:Safari为此问题提供了更多信息:

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

1 个解决方案

#1


0  

I can't be 100% sure, but it seems to me that the issue involved cross-site communication. What I ended up doing was having a PHP script download the file then I used javascript to get it locally.

我不能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();

#1


0  

I can't be 100% sure, but it seems to me that the issue involved cross-site communication. What I ended up doing was having a PHP script download the file then I used javascript to get it locally.

我不能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();