jQuery AJAX Success

后端 未结 6 1443
独厮守ぢ
独厮守ぢ 2021-01-17 10:08

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\         


        
相关标签:
6条回答
  • 2021-01-17 10:18

    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
    0 讨论(0)
  • 2021-01-17 10:25

    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 :)

    0 讨论(0)
  • 2021-01-17 10:33

    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 讨论(0)
  • 2021-01-17 10:34

    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.

    0 讨论(0)
  • 2021-01-17 10:36

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

    0 讨论(0)
  • 2021-01-17 10:39

    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.

    0 讨论(0)
提交回复
热议问题