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?
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.
-
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"); }
});