tags:

views:

37

answers:

2

Hi guys,

This might spund a little bit funny, didn't even know how to put the title at first. It could be due to long hours of work or I'm just doing something wrong.

I have a file, say comment.php, which contains something similar to:

var params = $('form#myform').serialize();
$.get("/ajax/file.php?"+params, function(data){
    if (data == 'error') {
        $('#message').html('Error');
    } else if (data == 'success') {
        $('#message').html('Success');
    }
});

I checked it with firebug and everything is ok, no JS errors, the file is being called and returns either 'error' either 'success', but still, in the body of the ajax call data doesn't match the server response. I then changed

if (data == 'error') {
    $('#message').html('Error');
} else if (data == 'success') {
    $('#message').html('Success');
}

with

if (trim(data) == 'error') {
    $('#message').html('Error');
} else if (data == 'success') {
    $('#message').html('Success');
}

where trim() is a function I wrote that removes any spaces before or after the string. I also tried wit the file being called echoing numbers and making the check with jQuery like:

if (data == 1) {
    $('#message').html('Error');
} else if (data == 'success') {
    $('#message').html('Success');
} 

with no results. If I only alert(data) everything looks fine tough. Both files are on the same domain. My code is exactly as in the example just that I have more if/else conditions and one of them should allways match (it does if I look in firebug). I also tried using a switch statement with no result.

I've been using this method for some time now and never had any problems. Ideas?

+2  A: 

The .get() callback callback uses 3 arguments:

  1. the data
  2. the status
  3. the XMLHttpRequest

So, you're method should work. Make sure you pay attention to capitalization, punctuation, and whitespace.

I would try and get more info about data to pinpoint the problem:

var params = $('form#myform').serialize();
$.get("/ajax/file.php?"+params, function(data){    
        alert("Type: " + typeof.data +
              "\nContents: ==>" + data + "<==" +
              "\nTrim: ==>" + $.trim(data) + "<==");  // get more info
});

Note that jQuery has a $.trim() function

Peter Ajtai
I don't think it actually answers the question why comparison fails: _"If I only alert(data) everything looks fine tough."_
Nikita Rybak
Thanks for the tip, but as Nikkita said, this isn't my problem. The file I'm calling actually echoes a string, which can be any of 'email', 'name', 'message' and so on. When I call alert(data) in my callback function, it does say 'email', 'name' or whatever, but it will fail on comparison, like if (data=='email') alert('email'); And thanks for the $.trim() function tip :)
Claudiu
@Claudiu - That's odd. I'd try and find out more about `data`. Just alert it with various bits of info, that should make you see why the match isn't working. Are you sure you got the spelling and capitalization right?
Peter Ajtai
@Nikita - Updated.
Peter Ajtai
Doh, my bad, I saw your suggestion on $.trim() but for some reason I ignored it. Works now! Any idea on how the jQuery trim works? My function worked good until now, by comparing the first character and removing it if it was equal to a space, same for the last characters, in a loop... Something I'm missing?
Claudiu
@Claudiu - Space isn't the only whitespace. Could be a tab or linebreeak. Also I would suggest a [ **regex** ](http://www.regular-expressions.info/examples.html) for trimming whitespace ( `[\s]` ).
Peter Ajtai
@Peter I see, never though this applies even when having something like _text = text.substring(1, text.length);_ Thanks for the help!
Claudiu
@Claudiu - trim spaces and tabs ==> `string.replace(/^[ \t]+|[ \t]+$/g, "")` ------------ spaces, tabs, and lines breaks ==> `string.replace(/^[ \s]+|[ \s]+$/g, "")` ------- You're welcome ;)
Peter Ajtai
+1 for effort invested :)
Nikita Rybak
A: 

You don't have to append the serialized data to the query string like that. $.get() will take three arguments, one of them is a serialized (query string) version of the data or a hash. However, that is not why you are here.

The data is just whatever the php page printed out. Are you actually printing out the word "error" or "success" from that page? If not, then what you are trying to do is not going to work.

The success callback takes three arguments: data, textStatus (response), and xhr. How about:

$.get("/ajax/file.php", $("#myform").serialize(), function (data, status) {
   if (status == 'error') {
      $('#message').html('Error');
   }
   else if (status == 'success') {
      $('#message').html('Success');
   }
});
tandu
You do realise you're basically telling me the same thing as Peter, right? Still, thanks for the help with sending the parameters, I tought the only other solution is with $.get('file.php', {key:value}....
Claudiu
Sorry, we posted at around the same time. I do realize there is an information overlap, but I thought my mentioning of the other parameters was quite relevant (I had that before his edit :)).
tandu