strange thing, ajax response includes whitespace

I ave several JS functions that call different php files with $.ajax jquery method... yesterday everything was fine, but after cleaning up my code, i don't know what i did, but now the ajax response is like "[space]data" instead of "data"..

i could use a trim function in Js but i want to fix the source of the problem...

all my php files have the missing last ?> in order to avoid that, and before <?php i'm sure, just checked, there is no space...

how come did I introduce this error? is the server? the browser?

The funny thing is that yesterday i cleaned my code with JSLINT..! bad idea..

thanks

Answers


When I had the same problem it was just a carriage return or space after the closing PHP tag, a surprisingly easy thing to introduce by accident.

Make sure you open the PHP tag at the start of the first line of your script, close it at the end and delete everything after the closed tag (should be easy to spot in a good editor).

I can see no reason why not closing your PHP tag wouldn't just be really annoying.. but thats just me!


I know I'm late this this thread, found it because I had the same issue. I was able to confirm that the although you may clear all the spacing in your callback function it is still possible to get these white-space/carriage returns in your response text (you can even see this in chrome developer tools under the "Network" tab).

So I tested and found that if you put ob_clean(); at the top of your callback function then it clears any of those spaces. I don't know much about this function just that it was mentioned in the codex (http://codex.wordpress.org/AJAX_in_Plugins#Debugging). Hope that helps anyone else that find there way here because of the same issue


If your response from the server includes the extra space, then the php code is the cause. If your response does not include the extra space, then the problem must be in javascript.

Use Fiddler to examine the actual response from the server to see if it really has the space in it. You'll then know for sure if the problem is with PHP or JS. Then you can post the relevant code.


Need Your Help

What is the difference between Model.findOne() & Model.findById() in Mongoose?

node.js mongodb mongoose objectid

Consider we are searching a document from MongoDB based on the _id value. Which one of the following code is efficient ?