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\
I guess that dataCheck
is a String. Then use localeCompare()
if (dataCheck.localeCompare('value') == 0) {
//Do stuff
}
str2
str1
is sorted after str2
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 :)
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.
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.
if(response.indexOf("success")!=-1){ } Try this method indexOf()
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.