DLH
DLH

Reputation: 2821

AJAX callback not handling data properly

I have a PHP script that I'm calling with the .post() function in jQuery. If everything goes well, it outputs "WIN", otherwise it outputs whatever database errors or whatever else it gets.

$.post("myscript.php", {key: "value"}, function(data) {
   if(data=="WIN") {
      // the stuff that I want it to do that it won't do
   } else {
      alert(data);
   }
});

When it runs however, I get "WIN" in a JS alert, and the stuff that I want it to do doesn't happen. Since "WIN" shows up in the alert, the PHP script is clearly outputting what I had expected. I had even made sure to set the Content-Type of the PHP script to text/plain, so why doesn't data=="WIN". Why does my WIN FAIL?

Upvotes: 0

Views: 87

Answers (2)

Nick
Nick

Reputation: 19684

You are most likely trying to access the returned data from 'data' directly. Try

 alert(data.d);

I think that you will find that the returned data is in a property. If I remember correctly, that property's name is 'd'. Use the FireFox plugin FireBug to view the returned data. It is very helpful for AJAX debugging.

Good Luck!

Upvotes: 0

Kristoffer Sall-Storgaard
Kristoffer Sall-Storgaard

Reputation: 10636

Have you checked for whitespace? Any whitespace before or after echo "WIN" will affect the output, also, check for whitespace before and after your <?php tags.

Upvotes: 1

Related Questions