8

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

Francesco
  • 24,839
  • 29
  • 105
  • 152

3 Answers3

20

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!

Codecraft
  • 8,291
  • 4
  • 28
  • 45
  • yes I thought that...in fact i had to go trough all children and parent files and i finally found the extra f** space!!! thanks – Francesco Mar 29 '11 at 18:53
  • 2
    PHP treats EOF as equivilent to a closing PHP tag. Thus a closing PHP tag at the end of a file is redundant. And, as you have discovered, it is error prone. Besides breaking AJAX requests, you can send some versions of IE into quirksmode if you get any whitespace before your DOCTYPE. In my day job we make it a habbit to leave off the closing PHP tag, and I never use it in my personal work. – JAAulde Mar 17 '12 at 12:56
  • I also found UTF BOM at the start of all the included files were adding spaces. I resaved the files without the BOMs – Sam Strachan Jul 17 '15 at 16:20
  • Continuing @SamStrachan UTF-8 encoding especially can cause this as is outlined here: https://stackoverflow.com/questions/10199355/php-include-causes-white-space-at-the-top-of-the-page – Jonathan Aug 04 '17 at 11:27
12

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

Xtremefaith
  • 907
  • 1
  • 9
  • 29
0

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.

Brandon
  • 38,310
  • 8
  • 82
  • 87