Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 515
Francesco Avatar asked Mar 29 '11 18:03

Francesco


2 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!

like image 131
Codecraft Avatar answered Nov 10 '22 17:11

Codecraft


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

like image 35
Xtremefaith Avatar answered Nov 10 '22 15:11

Xtremefaith