0

I have a large amount of data I am sending via an XMLHttpRequest POST that I am saving to a database. The php file I am posting to is entirely PHP code. I wanted to add some sort of return value in the responseText so I could tell the user if the data saved properly, but I noticed something strange. I was doing some testing and I was just using echo to post some basic text that I could check from the original page in order to send an alert to the user.

To make a long story short, my check was failing because the responseText was including 2 empty spaces, so my echo "success"; was padded with 2 empty spaces. As I said, the entire file is php with no html at all, or any other echo. The string count with no echo at all of the reponseText is 2, both empty spaces. I have done a simple test using GET and it did not do the same thing. The responseText only included the characters I echo'd.

Here is the POST code:

HttpRequest.open('POST', "save_message.php", true);
HttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttpRequest.setRequestHeader("Content-length", param.length);
HttpRequest.setRequestHeader("Connection", "close");
HttpRequest.send(param);

I assume the actual parameters don't matter as they shouldn't be writing anything to the document. I also did not include the php for the requested page as it is fairly large and I have been through it about 50 times, and it is not echoing anything to the page at all, so I am really confused. Any idea what could be causing my POST to return 2 extra chars? Any input would be greatly appreciated as this is driving me crazy.

Thanks.

  • Does your PHP script use a closing `?>` tag? If so, white space can be sent afteward. Still doesn't explain GET vs. POST disparity, though. – user1091949 Apr 07 '13 at 14:43
  • Yes, I did have a closing tag. I removed it and tested again and I am still getting the 2 empty spaces as my responseText. Was worth a shot! – Rand Al' Thor Apr 07 '13 at 14:54
  • Odd. Well trimming white space from the `responseText` will fix the character count and the problem. Doesn't explain it, though. http://stackoverflow.com/a/498995/1091949 – user1091949 Apr 07 '13 at 15:17

0 Answers0