views:

1436

answers:

3

I'm developing an iPhone web app with dashcode. My app runs fine when I browse it with the iphone simulator. When I deploy it to a web-server, I never get a response from XMLHttpRequests. Here's the code I use:

function get(feedURL, handler) {
    var onloadHandler = function() { handler(xmlRequest); };    
    var xmlRequest = new XMLHttpRequest();
    xmlRequest.onload = onloadHandler;
    xmlRequest.open("GET", feedURL);
    var credentials = encodeBase64(_login.username + ':' + _login.password);
    xmlRequest.setRequestHeader("Authorization", "Basic " + credentials);
    xmlRequest.send();
}

When I attach a onreadystatechange handler, I can see the the request goes to state 4. I can also see the requests in my server logs. I also added a "no cache" request header, which didn't help. I tried this on a local web-server on a hosting package that I have and I don't have any luck getting it to work.

A: 

Maybe try the new debugging capabilities that are part of DashCode 3.0 (new with Snow Leopard)? Not really an answer, but perhaps a change of scenery -- or the new debugger -- will shake something loose.

slothbear
+1  A: 

The iPhone probably isn't expecting to receive Basic Auth headers, since you manually set the request header. Try adding the username and password to the URL (after percent encoding) and run the request that way.

Douglas Mayle
+2  A: 

A shot in the dark, but the apple docs say this.

the domain of the URL request destination must be the same as the one that serves up the page containing the script. This means, unfortunately, that client-side scripts cannot fetch web service data from other sources, and blend that data into a page. Everything must come from the same domain. Under these circumstances, you don't have to worry about security alerts frightening your users.

What are the values of status and statusText? Also try getAllResponseHeaders() for more info.

Also, if it's just basic auth you want, you can just pass it to open() like so:

open("method", "URL", asyncFlag, "userName", "password")
slf