【问题标题】:ready-to-use xmlRequest snippet returns status code 0 on a call即用型 xmlRequest 片段在调用时返回状态代码 0
【发布时间】:2009-12-21 16:06:37
【问题描述】:

我正在创建一个非常基本的 dashcode 小部件。我正在使用来自 Apple 的定制 sn-ps:xmlRequest setup 和 xmlLoaded。下面是我的代码

function sendURL(){
    // Values you provide
    var textField = document.getElementById("searchqeue");  // replace with ID of text field
    var textFieldValue = textField.value;
    var feedURL = "feed://isohunt.com/js/rss/"+textFieldValue+"?iht=&noSL";                     // The feed to fetch
    
    // XMLHttpRequest setup code
    httpFeedRequest = new XMLHttpRequest();
    
    function loadedXML()
    {
        alert("xmlLoaded");
        if (httpFeedRequest.status == 200)
        {
            // Parse and interpret results
            // XML results found in xmlRequest.responseXML
            returnvalue = httpFeedRequest.responseText;
            alert(returnvalue);
            var textField = document.getElementById("resultarea");
            textField.value = returnvalue;
            // Text results found in xmlRequest.responseText
        }
        else
        {
            alert("Error fetching data: HTTP status " + httpFeedRequest.status);
        }
    }
    httpFeedRequest.onload = loadedXML();
    alert ("onload executed");
    
    //httpFeedRequest.overrideMimeType("text/html");
    httpFeedRequest.open("GET", feedURL);
    httpFeedRequest.setRequestHeader("Cache-Control", "no-cache");

    // Send the request asynchronously
    httpFeedRequest.send(null);
    alert("sent");
}

我想把它全部放在一个函数中以保持简单。我当然可以将 xmlLoaded 函数和 sendURL 函数分开,但对我来说这更清楚。

我在错误控制台中返回的代码:

xmlLoaded(loadedXML 函数内第一行)

获取数据时出错:HTTP 状态 0(来自 loadedXML 函数的错误消息)

onload 执行(httpFeedRequest.onload 下面的行 = loadedXML();)

发送(函数的最后一行)

esn-ps 本身带有 dashcode,所以我想问题出在我的网址上。这是一个 feed:// 页面,而不是 html 或 xml 页面。但由于提要也是 xml,我虽然可以只使用 xmlRequest。此外,使用 http:// 而不是 feed:// 调用同一页面会返回相同的结果。

httpFeedRequest.responseText 返回 ""(空字符串)

httpFeedRequest.responseXML 返回 null

任何帮助将不胜感激!

【问题讨论】:

  • 同源策略是否应用于dashcode小部件?

标签: javascript xmlhttprequest widget feed dashcode


【解决方案1】:

您现在可能已经对此进行了排序,但有几件事。 httpFeedRequest.status == 200 用于 http 响应。如果您正在调用提要,那么它可能不会给出相同的数字响应。如果您使用 Safari 并进入开发人员模式,然后在您的脚本上设置一个断点并遍历它,您可以看到返回来检查这一点的响应。 此外,您没有检查请求的就绪状态 - if(myRequest.readyState == 4)

就绪状态是 数字; 0 表示未初始化,open() 尚未被调用; 1 表示加载,send() 没有被调用; 2 表示已加载,已调用 send(),并且标头/状态可用; 3 表示交互,responseText 保存部分数据; 4 表示完成。

所以通常检查 4。

在 httpFeedRequest.open("GET", feedURL);您可以尝试添加 true 参数来告诉请求它是异步的 httpFeedRequest.open("GET", feedURL, true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多