views:

604

answers:

1

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!

+1  A: 

you have probably sorted this by now but a couple of things. httpFeedRequest.status == 200 is for http response. if you are calling a feed then it may not give the same numeric response. If you use Safari and go into developer mode and then set a breakpoint on your script and walsk through it you can see the responses that come back to check this. Also you are not checking the ready state of the request - if(myRequest.readyState == 4)

the readystates are Number; 0 means uninitialized, open( ) has not yet been called; 1 means loading, send( ) has not been called; 2 means loaded, send( ) has been called, and headers/status are available; 3 means interactive, responseText holds partial data; 4 means completed.

so it is usual to check for 4.

And in httpFeedRequest.open("GET", feedURL); you might try adding the true parameter to tell the request it is asynchronous httpFeedRequest.open("GET", feedURL, true);

PurplePilot