Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

REST API and JSON and Readystate

Hi

I built a REST API using PHP.
My iphone app POST json data to the api but never got back readystate == 4.

I am returning http status=201 and a content containing json response but the

readystate is either 1 or 2 but never 4.
And the responsetext is null.

What did I miss here?

— asked June 5th 2010 by Peter Lum
  • api
  • json
  • readystate
  • rest
0 Comments

2 Answers

  • If you can show the code you are using (try http://pastie.org) then others can try and help out. Without the code it's hard to diagnose the problem.

    — answered June 5th 2010 by Angus Peart
    permalink
    0 Comments
  • Here are the iphone snippet:

    if (Titanium.Network.online)
    {

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onerror = function(e)
    {
    Ti.API.info("ERROR " + e.error);
    actInd.hide();
    alert(e.error);
    };
    xhr.onreadystatechange = function()
    {
    // label.hide();
    try {
    if (this.readyState == 4)
    {
    // var results = JSON.parse(this.responseText);
    alert('State= ' + this.readyState);
    ndpc_status = this.responseText;

                    label.text = ndpc_status;
    
    
                    }
                } catch(e)
                    {
                    Ti.API.debug(e.error)
                    alert(e.error);
                    }
    
    
    
    
    
    
            actInd.hide();
        };
        // open the client and encode our URL
        xhr.open('POST','https://www.fgg.com.sg/');
    
        // base64 encode our Authorization header
        //xhr.setRequestHeader('Authorization','Basic '+Ti.Utils.base64encode(username.value+':'+password.value));
    
        // send the data
        //xhr.setTimeout(100);
        xhr.setRequestHeader("Content-Type","application/json");
    
        var params = {"action":"register","nick":NDPN_Nickname,"user":NDPN_Username,"pw":NDPN_Password};
    
        var paramsStr = JSON.stringify(params);
        xhr.send(paramsStr);
    

    });

    }
    else
                {
                alert("Sorry, there is no network available");
                }
    

    });

    — answered June 5th 2010 by Peter Lum
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.