views:

171

answers:

1

I am trying to submit an xhr using phonegap on the blackberry. Currently when I make the request it fails silently. I have tried using jQuery jQuery.getJSON(url , callback),

an xhr object

var xmlhttp = new XMLHttpRequest();

//callback function when XMLHttpRequest is ready
xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4){
        if(xmlhttpForTGT.status==200){

            response = xmlhttpForTGT.getResponseText()
        }
        else{
            alert("Request Failed")
        }
    } 
}

xmlhttp.open("GET", url , true)
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
xmlhttp.send()

, and xui

x$('#home').xhr(url, 
    {method: 'get',
    callback: function(){ alert('made request') }});

and so far I can't get any of them to work. Has anyone else been able to get xhr's to work? I'm using JRE4.6.1 & using eclipse for the development environment. No errors are thrown when I attempt to make the requests, any advice??

+1  A: 

I've contributed a lot to the phonegap-blackberry project, and in all of my tests, for some reason the XmlHttpRequest object always raises an empty exception when you call the open() function on it. I'm not sure why - phonegap-blackberry in its current form leverages RIM's proprietary, non-WebKit browser, so maybe that has something to do with it.

What I've done though is implement a very basic, native, Java-based approach to making HTTP requests and retrieving the response. It is bridged back into JavaScript in your PhoneGap app as part of the 'network' PhoneGap API. Some information about that is here: http://wiki.phonegap.com/Known-issues-(BlackBerry)

NOTE: this is most definitely a stopgap measure. The implementation is rough and could definitely use some work. Currently, it only works with API endpoints that return JSON.

Example usage:

navigator.network.XHR('http://www.mysite.com/myapi',
    'This is my POST data, or I could pass in "null" for empty POST data',
    function(response) {
        // This is my success callback.
        // Do something with the JSON response object here.
    });

Hope that helps.

fil maj
Yes that definitely helps, thanks. I had seen the issues for blackberry, but wasn't sure about the specifics behind it. With that implementation, is there any way to set request headers?
a.meservy
No. Headers are set on the native end in Java, see http://github.com/phonegap/phonegap-blackberry/blob/master/framework/src/com/nitobi/phonegap/api/impl/NetworkCommand.java#L200For a quick fix, you should be able to set them in Java for your needs, or, feel free to extend this so that you can set it via JavaScript. If you do, make sure you send me a pull request on github (github.com/filmaj) and i'll be sure to include it back into the master!I really want to scrap this implementation and just write the full XHR API properly. Let me know your thoughts on this, perhaps we can combine efforts.
fil maj