Titanium Community Questions & Answer Archive

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

XHR - null response text/data but status code of 200.

I am experiencing the exact same issue as two other people on this board. I keep getting back a null value from a POST request.
even if i send no data to the server, the server will always return something.

I have tried stringify-ing the json object, building the json object at different points, and sending it any way i can think of under the sun.
i am getting a http status code of 200 but no data. i know this server works as i have tested it xcode, objC code and in my browser.

I've followed everything verbatim from examples and i keep getting null data back on a POST.

    var xhr = Titanium.Network.createHTTPClient();
    xhr.open('POST','some url');
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    //ERROR
    xhr.onerror = function(e) {
        alert("ERROR " + e.error); 
         Titanium.API.info("some error");
    }; 
    //SUCCESS
    xhr.onload = function() {
        //callback(JSON.parse(this.responseText));
        Titanium.API.info(this.responseText);
        Titanium.API.info(this.status);  
    };
     var temp = {};
     temp.method ="user.login";
     temp.username="admin";
     temp.password="red99";
    xhr.send(temp);

I'm the third person to post about this issue in the last few days or so. seems like something isn't quite right there.

im using version 1.2.1

— asked April 23rd 2010 by eaymon latif
  • iphone
  • json
  • mobile
  • xhr
0 Comments

6 Answers

  • I hesitate to point out something so obvious, but the parse code above is on a comment line…

    regards,

    JH

    — answered April 25th 2010 by John Holman
    permalink
    0 Comments
  • Just for arguments sake… i did the following where you are suggesting the parse code should be (commented out on purpose):

    Titanium.API.info(this.responseText);
    Titanium.API.info(JSON.stringify(this.responseText));
    Titanium.API.info(JSON.parse(this.responseText));

    i got:

    [INFO] <null>
    [INFO] null
    [INFO] <null>

    i receive text back from the server, i should only need to parse if i wish to handle the JSON text as an object. at this point i would be happy to see any response ….

    — answered April 25th 2010 by eaymon latif
    permalink
    0 Comments
  • Even I am facing the same issue with only one exception. When I try to display the response using alert then it correctly shows the response sent by server. Even you can try alerting it. But whenever I parse the JSON response or do anything else it always gives a null value.

    — answered May 7th 2010 by Dharani Karthikeyan
    permalink
    0 Comments
  • I'm having the same problem. If you send with 'POST' apache doesn't see the request, but if you use GET, the servers sees the request correctly and handles it correctly.
    I'm using the latest TI.SDK 1.3.0

    Any ideas?

    — answered May 24th 2010 by Awesome Dude
    permalink
    0 Comments
  • I've got same issue (answer from http after a POST request) if the answer contains non UTF8 characters !
    So be sure that's your answer is UTF8 encoded

    — answered August 22nd 2012 by Yoann WYFFELS
    permalink
    0 Comments
  • I think I found a possible solution to this.
    I spent 4h today trying to solve this issue.
    Here is what I found so far:

    1. If you :
      var jData = JSON.stringify({'a':'content'});
      
      and then try to send that with
      xhr.send(jData);
      
      You will get <NULL> from the server
      But, if you try:
      xhr.send({'a':'content'});
      
      Then you get a response from the server with anything you wanted back.
      Not sure why that is, but that's what I found.
    — answered May 25th 2010 by Edwin Huertas
    permalink
    1 Comment
    • i have the same problem like you but i coudn't understand your solution because i don't understand what is '{'a':'content'}'

      — commented January 31st 2014 by mobile webdev2014
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.