user697286
user697286

Reputation: 41

Problem sending AJAX request with headers on Blackberry Webworks

I am developing a Blackberry webworks application and I am having trouble with an AJAX request that I am making to a server. I am learning HTML/Javascript/AJAX on the fly, so excuse any beginner mistakes. Basically, formatted HTTP requests are made to the server, which returns JSON objects that I use in the application. I am using AJAX to make the requests without any kind of framework. Most requests do not have to be authenticated, and those are returning just fine. However, to access a directory part of the server, a username and password are encoded and sent as a header with the XMLHTTPRequest. when I try and add the header, the request is sent, but I never get anything back. The readyState property is set to 1, but never goes beyond that. I know the server works fine, because I did the same thing for iPhone, and it worked.

Here is the relevant code:

function grabFromServer(httpRequest){   
httpConnection = new XMLHttpRequest();
var me = this;
httpConnection.onreadystatechange=function(){
    alert(httpConnection.readyState);
    if(httpConnection.readyState==4){
        me.processResponseText(httpConnection.responseText);
    }
};
httpConnection.open("GET", httpRequest,true);

if(this.request == "company" || this.request == "property" || this.request == "individual"){
    var authorized = this.checkCredentials();
    if(!authorized){
        //ask for username pword
    }
    //here, add credentials
    httpConnection.setRequestHeader("Authorization", "Basic : ODI5ZGV2bDokY19kdXN0Ym93bA==");
}
httpConnection.send();

}

Upvotes: 4

Views: 1338

Answers (1)

Core.B
Core.B

Reputation: 662

Your code appears to be good. Have you added an entry in your config.xml file to allow access to your domain? You should see an entry for something like <access subdomains="false" uri="http://data.mycompany.com/"/>. To make any HTTPRequests to an external website from a WebWorks application, you have to add an entry to "whitelist" domain like this.

If you're using the eclipse plugin, open up the config.xml file, click the Permissions tab at the bottom, and click "Add Domain".

Upvotes: 1

Related Questions