jquery: If statement not working inside ajax success function

前端 未结 3 1542
闹比i
闹比i 2021-01-05 16:51

I have a success function in my AJAX which returns the response text from a python script which can be either \"SUCCESS\" or \"EMPTY\". Now I want to place an if-loop insid

相关标签:
3条回答
  • 2021-01-05 17:31

    This means that what you're responding with isn't "SUCCESS". It probably has a line break or other whitespace before or after it, maybe more than one.

    Perhaps:

    if (/^\s*SUCCESS\s*$/.test(data)) {
        // Success
    }
    

    Or use jQuery's $.trim:

    if ($.trim(data) === "SUCCESS") {
        // Success
    }
    

    Also be sure that you're not replying with "Success" or "success", as string comparisons are case-sensitive.

    If you're not sure:

    if (/^\s*SUCCESS\s*$/i.test(data)) {
        // Success
    }
    

    or

    if ($.trim(data).toUpperCase() === "SUCCESS") {
        // Success
    }
    
    0 讨论(0)
  • 2021-01-05 17:40

    check if you don't have any additional spaces before/after "SUCCESS"

    And also try to change

    if(data === "SUCCESS"){
    

    to

    if(data == "SUCCESS"){
    
    0 讨论(0)
  • 2021-01-05 17:50
    submitHandler: function (form) {
    
                $.ajax({
                    type: 'post',
                    url: '/cgi-bin/getdataworld.py',
                    data: $(form).serialize(),
    
                    success: function(data1) {
                            //document.write(result);
                            console.log("result is "+data1);
                            alert(data1);
    
                            if(data1 == "SUCCESS"){
                            window.location = 'index.html';
                               }
                           else{
                                 alert("NO DATA PRESENT");
                               }
    
    
                    },
    
                    error: function (responseData) {
                console.log('Ajax rariequest not recieved!');
            }
    
                });
    
                return false;
            }
    

    try this here two things i noticed one is === in if statement and other you are using data as variable some time its conflicts in some browser

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