1

I think this is just an obvious problem that I can't spot because I've been staring at code too long today. I have this callback as part of a JQuery POST:

                        function(data){
                                if(data == 'yes') {
                                        $('div.done').show();
                                } else if(data != 'yes') {
                                        $('div.error').show();
                                        alert(data);
                                }
                        });

I get the error div shown, yet in the alert all I get is yes. Anyone got any pointers (as I said, I imagine it's some really obvious mistake that I've missed).

Thanks

6
  • try this with firefox: alert(data.toSource()) Commented Dec 24, 2009 at 12:45
  • 1
    are you sure that data has no whitespace at it's begin/end? try adding data = data.replace(/^\s+|\s+$/, ''); before if statement. Commented Dec 24, 2009 at 12:47
  • Good suggestions, just thought I'd pipe in with if he's using jQuery he might as well use $.trim(data) ;). Commented Dec 24, 2009 at 13:04
  • Thanks for the comments. The data.replace one didn't work, neither did $.trim(data). Just somebody, I can't seem to get that to work. What would I do, just put javascript:alert(data.toSource()) in the URL bar? Commented Dec 24, 2009 at 13:12
  • @hrickards: no, I meant you should augment the alert call in the code you posted. Object.toString is non-standard and I told you to run it in firefox which does have it. what exactly does "can't seem to get that to work" mean? Commented Dec 24, 2009 at 13:23

1 Answer 1

4

Debuggng 101:

function (data) {
    alert(escape(data));
    if (data == 'yes') {
        $('div.done').show();
    } else if (data != 'yes') {
        $('div.error').show();
        alert(data);
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. That turned up yes%0a. Is that a line feed? Do you know how I could remove that, or even better stop PHP sending it? The only thing in my PHP file that sends anything is echo 'yes';.
%0A is a line feed you can do a regular expression: data.replace(/\n+$/);
Thanks. Any idea why that might be being sent?
Hmm, for some reason I still have the same issue. alert(escape(data)); still shows yes%0a.
The reason PHP is sending an extra line feed is probably because you have whitespace after your closing ?>. Delete the whitespace, or better yet, delete the entire closing tag (it's optional).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.