0
var xhr=Titanium.Network.createHTTPClient({
    onload:function(e){
    if(this.status == '200'){
          Ti.API.info('got my response, http status code ' + this.status);
           if(this.readyState == 4){
                var response=JSON.parse(this.responseText);
                success = true;
                }
            else{
                alert('HTTP Ready State != 4');
            }           
        }
        else{
            alert('HTTP Error Response Status Code = '+this.status);
            Ti.API.error("Error =>"+this.response);
        }              
    },

});
xhr.onerror = function(e){ 
};

xhr.open("POST","http://localhost:23003/api/user?username=dp&password=123456" ,true);//ADD your URL

xhr.setRequestHeader("content-type", "application/json");

xhr.send();  // Taa da

Hi there, The above mentioned is my code, I am trying to send a post request to a local server process as mentioned in the open method. But I used some console messages to see these to objects that were returned

  1. this.responseText -- This text is blank. (I know java script returns null differently and the alert message displays 'null' as null.)
  2. this.status -- returns '0'
  3. this.readyState returns 4
  4. var response = JSON.parse(); is a blank text
  5. I have tried changing the content type to -application/x-www-form-urlencoded too but I get the same results

I have no idea where I'm wrong. Any help will be appreciated. Thank you for your time.

1
  • 1
    a/ you could try to change localhost in url to your ip address. b/ try checking the the webservice using postman to confirm that webservice is working fine.
    – turtle
    Mar 4, 2015 at 6:44

1 Answer 1

2

It looks like something is wrong with your server-side code because the following:

  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
      console.info(this.status);
      console.info(this.readyState);
      console.info(this.responseText);
    }
  });
  xhr.open("POST", "http://requestb.in/ynwa0gyn");
  xhr.setRequestHeader("content-type", "application/json");
  xhr.send();

Correctly gives:

[INFO]  200
[INFO]  4
[INFO]  ok

Visit http://requestb.in/ynwa0gyn?inspect for the request made by the client.

4
  • Do you think it could be something to do with my local firewall or something else? Mar 4, 2015 at 23:26
  • It could. You'll have to find a way to see how the request comes in on your server and see if that is correct or not. Mar 5, 2015 at 7:48
  • Do you think a browser security setting or a windows security setting is doing this? Mar 5, 2015 at 15:35
  • Its a CORS issue. I went through this html5rocks.com/en/tutorials/cors. I have to set up the server to handle a CORS request and client should include a few extra headers mentioned in the link. Or one can use the android or Iphone simulator/emulators to run this. This is a purely browser security hurdle. Mar 13, 2015 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.