1

Im seeing some very odd stuff in a simple ajax/jquery code I wrote and I can't explain why Im seeing what Im seeing.

I have a php file called complete.php, the only thing this does at the moment is:

echo "saved"; 

I have a jquery ajax call, which has a .done() function, which looks like this:

jQuery.ajax({ type: "POST", url: "complete.php", data: "id=someIdHere", }).done(function(d){ alert(d); if(d == "saved"){ alert('loading done'); }else{ alert('error'); } }); 

My firebug confirms the response from the complete.php is 'saved'.

when I typeof the 'd' response, it is of type 'string',

however for some reason, the alert I get in the .done() function, is 'error' and not 'loading done'.

the 'alert(d)' just before the if statement gives 'saved'.

What am I missing here?

6
  • So if logged the typeof d sucessfully as "string", have you tried logging d itself? What is it? Commented Jan 22, 2013 at 12:57
  • yes, it gives 'saved' as I though it would/should Commented Jan 22, 2013 at 12:58
  • Using firebug, place a breakpoint in the .done() function and check the value of d. There might be more than just what you hope. Commented Jan 22, 2013 at 12:58
  • 1
    try console.log(d) to see what it might be. It could be you need to do if (d.toString()=="saved")... Commented Jan 22, 2013 at 12:59
  • 1
    I can't see why it should, but may be worth checking there isn't any additional whitespace being added to d: if($.trim(d) == "saved") Commented Jan 22, 2013 at 13:08

3 Answers 3

1

You should use a more robust return type, such as JSON:

header('Content-Type: application/json'); echo json_encode(array( 'status' => 'saved', )); 

Then in your success handler:

if (d.status == 'saved') { ... } 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I fully agree. I was wrote the code just as a means to confirm the response from the php script, then couldn't understand why it was not turning as expected, so though it made sense to put it here in case anyone met the same issue. production code will be json
0

try

jQuery.ajax({ type: "POST", url: "complete.php", data: "id=someIdHere", success: function(){ alert('loading done'); }, error: function(){ alert('error'); } }); 

2 Comments

I was just about to suggest this as the success function would be fully managed by the ajax rather than rely on the jqXHR implementation. You can also use your variable in the success...
You can also use the complete option of the ajax method if you need to do something in both both cases.
0

Very odd but Iv found the issue.

my php was

echo "saved"; 

which is right, however when I debugged the return as per @mplungjan suggestion, the return was " saved" (with white space) this was odd as the php had no white space, I then changed the php to

echo trim("saved");

but when I again debugged the return, it again was " saved" (with the white space)

No idea why the white space is being applied, but I have resolved it by replacing the white space in the return / .done() function

I wont select this as the answer, but if anyone knows hoe the white space could have been added Id love to know.

Im using firefox 18 on mountain lion

2 Comments

any whitespace before the <??
@dan-dvorak there was. Iv removed this. still comes back with the whitespace before the 'saved'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.