开发者

GET HTTPs request not working properly in phonegap

开发者 https://www.devze.com 2023-04-06 10:20 出处:网络
I\'m running this app in an Android phone (Samsung Galaxy mini running on ANdroid 2.2). I\'m using couchdb for my database and host it on cloudant.

I'm running this app in an Android phone (Samsung Galaxy mini running on ANdroid 2.2). I'm using couchdb for my database and host it on cloudant.

On device ready, the app will make a get request to the https db server which contains feeds of data that i need to be displayed in my app. At first, i开发者_如何学编程t was working fine but when i try to post and add new data to my https db server, then trigger the get request method, the returned response from the server is still the same as before(the newly posted data was not included even though i checked my db server and saw that it was indeed saved). Then i tried to close and reopen the app again, which will then make a new instance of the get http request, but still, the response still is the same as the very first one and doesnt contain the new data that was added to the db server. Then, I tried to reinstall my app, then run the app again, and oddly enough, the response from the get request now contains the new data.. I don't know how that happens, and I'm not really experienced with REST api and javascript so I might be doing something obviously wrong. Here's a snippet of my code for the get request:

var getFeedsClient;

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

    if (window.XMLHttpRequest) {
        getFeedsClient=new XMLHttpRequest(); 
        }
    else {
        getFeedsClient=new ActiveXObject("Microsoft.XMLHTTP");
        }

    try{ 
        getFeedsRequest();
    } catch(e)
    {
    alert("Error on device ready: "+e);
    } 

}//on Device Ready




function getFeedsRequest() {
        getFeedsClient.onreadystatechange = getFeedsFunction;
         getFeedsClient.open("GET","https://<username>.cloudant.com/te/_design/requestfeed/_view/all",true);
        getFeedsClient.send();
}

function getFeedsFunction() {
    alert(" readystate: "+getFeedsClient.readyState+", status: "+getFeedsClient.status);
    if(getFeedsClient.readyState == 4 &&  getFeedsClient.status == 200 ) {
        var data = JSON.parse(getFeedsClient.responseText);
            console.log("RESPONSE FROM COUCHDB "+getFeedsClient.responseText);
            console.log("JSON data "+data);

        //at first the total rows is 2 but when a new data was added, the total rows should be three, but that was not the case
        alert("rows: "+data.total_rows);

    }
}  


I ran into a similar issue last week of the device caching requests. I solved it by adding a dummy unique parameter to the link like below:

function getFeedsRequest() {
    getFeedsClient.onreadystatechange = getFeedsFunction; 
    var link = "https://<username>.cloudant.com/te/_design/requestfeed/_view/all";
    var unique = (new Date).getTime();
     getFeedsClient.open("GET",link + '?' + unique,true);
    getFeedsClient.send();

}


Paul Beusterien's solution below worked for me with the following caveat:

An android 4.0.4. phone did not need a unique URL, but the same code running on 2.2.2 did!

0

精彩评论

暂无评论...
验证码 换一张
取 消