I'm using jQuery to make ajax requests. The data is getting to PHP nicely, but the response isn't getting back to javascript properly. Somehow there is a space before the response. I know this because Firebug says so and my code doesn't work because the space is there. When I expect there to be a space it works fine. Any ideas as to what could be adding the space?
Here is my ajax function:
function my_ajax (aurl, adata, aretfunc) {
$.ajax({
type: "POST",
url: aurl,
data: adata,
success: function(msg) {
eval(aretfunc+'(msg);');
}
});
}
Look for spurious whitespace characters outside of the <?php ?>
tags in your PHP file. Any such whitespace will get output when the PHP script is executed. If your PHP file includes other PHP files, the same thing applies to those files as well.
Agreed, look for spurious whitespace character outside of the <?php ?>
. One suggestion, and one that is completely legit is to simply remove the trailing ?>
, as they are unnecessary. In fact, it's a coding standard for Drupal.
In some cases, I've found that just running the response through $.trim() before doing anything can work fairly well.
Of course, the solutions above are still very applicable, but if you're in a situation where you can't change that, I figured it'd be worth throwing out there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With