15

I'm using jQuery's $.ajax function to submit a form, which works, but the success is where I'm having my problem. Here is my code:

$("#form").submit(function () {
        $.ajax({
            type: "POST",
            url: '/login/spam',
            data: formData,
            success: function (dataCheck) {
                if (dataCheck == 'value') {
                     //Do stuff
                }
            }
        });
        return false;
    });

The problem I'm having is the if function keeps saying that dataCheck doesn't equal value. I know that it does, because when I remove return false; the page displays value, as expected. Also, I have used an almost identical code before, which works. Could somebody give me some advice?

neex1233
  • 373
  • 2
  • 3
  • 16

6 Answers6

19

How to find the answer yourself:

Place a debug code to see what you get from the server.

$("#form").submit(function () {
        $.ajax({
            type: "POST",
            url: '/login/spam',
            data: formData,
            success: function (dataCheck) {
                console.log(dataCheck); // <==============================
                if (dataCheck == 'value') {
                     //Do stuff
                }
            }
        });
        return false;
    });

It will probably be in other format than you think.

scourge192
  • 1,909
  • 16
  • 22
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 2
    return false wont be working if there is an error in the script above return false statement. preventDefault does. So i prefer that over return false; http://stackoverflow.com/a/4771084/40521 – Shyju Apr 22 '12 at 03:25
  • Keep in mind that jQuery will try to infer the data type of the result (since you're not specifying it) based on the MIME type of the response. You might need to do you evaluation differently depending on the type. – Jeremy Smith Apr 22 '12 at 03:29
  • The benefit to using event.preventDefault() is that you can add this as the first line in the handler, thereby guaranteeing that the anchor's default behavior will not fire, regardless if the last line of the function is not reached (eg. runtime error). – Shyju Apr 22 '12 at 03:30
  • @Shyju. You wrote that already... :) And my response is that I'm not afraid of runtime errors. There shouldn't be error in my code. and return false also stop the bubbling. so... first line\last line? I don't care... – gdoron Apr 22 '12 at 03:32
2

If you want to prevent the default behaviour( in this case the normal form submit), use preventDefault over return false; preventDefault will work even if there is a problem in the script which is above return false statement.

The below code should work fine.

$("#form").submit(function(e) {
   e.preventDefault();
    $.ajax({
        type: "POST",
        url: '/login/spam',
        data: formData,
        success: function (dataCheck) {
            if (dataCheck == 'value') {
                 //Do stuff
            }
        }
    });    
});

As gdoron mentioned, use console.debug/ alert to see what value is in the variables. Using firebug Net tab / fiddler will help you to understand what response you are getting from the server page.

Maurice
  • 3
  • 4
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

It's strange. My advice is you could use firebug in firefox or something else (e.g. develope tools in chrome) to see the xhr response.

And I think maybe the wrong result cause by wrong type xhr response(e.g. '749afa42e6621f10bae17ee00cb1f4de' envelope with html tag) or some space not trimed.

May that help you :)

wedgwood
  • 1,064
  • 9
  • 10
0

I know it might be late however I was having this same issue today and the problem was that the correct string was returned to success, however for some reason it had an extra newline character in front of it. Locally this did not happen however when I put it up online a newline was always added to the string being returned (in your case dataCheck). I used the substr() function to get rid of the newline at the end and it worked fine from there on.

0

if(response.indexOf("success")!=-1){ } Try this method indexOf()

0

I guess that dataCheck is a String. Then use localeCompare()

if (dataCheck.localeCompare('value') == 0) {
    //Do stuff
}
  • Returns -1 if str1 is sorted before str2
  • Returns 0 if the two strings are equal
  • Returns 1 if str1 is sorted after str2
Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
Erik Lucio
  • 948
  • 7
  • 8