0

I'm creating a very basic dashcode widget. I'm using customized snippets from Apple: xmlRequest setup and xmlLoaded. Below is my code

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");
}

I want to keep it all in one single function to keep it simple. I could ofcourse separate the xmlLoaded function and the sendURL function but for me this is clearer.

the code I get back in the error console:

xmlLoaded (inside the loadedXML function first line)

Error fetching data: HTTP status 0 (the error message from the loadedXML function)

onload executed (the line beneath httpFeedRequest.onload = loadedXML();)

sent (the last line of the function)

Thes esnippets come with dashcode itself, so I guess the problem is with my url. This is a feed:// page instead of a html or xml page. But as feeds are xml too, I though I could just use the xmlRequest. Also, calling the same page with http:// instead of feed:// returns the same.

httpFeedRequest.responseText returns "" (empty string)

httpFeedRequest.responseXML returns null

any help will be more than appreciated!

4

1 回答 1

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);

于 2009-12-21T08:06:37.430 回答