Ajax success function

后端 未结 2 1466
慢半拍i
慢半拍i 2020-12-16 14:10

I am using an Ajax post to submit form data to the server, be validated and then return a message based on whether or not the data was valid and could be stored. My success

相关标签:
2条回答
  • 2020-12-16 14:26

    It is because Ajax is asynchronous, the success or the error function will be called later, when the server answer the client. So, just move parts depending on the result into your success function like that :

    jQuery.ajax({
    
                type:"post",
                dataType:"json",
                url: myAjax.ajaxurl,
                data: {action: 'submit_data', info: info},
                success: function(data) {
                    successmessage = 'Data was succesfully captured';
                    $("label#successmessage").text(successmessage);
                },
                error: function(data) {
                    successmessage = 'Error';
                    $("label#successmessage").text(successmessage);
                },
            });
    
            $(":input").val('');
            return false;
    
    0 讨论(0)
  • 2020-12-16 14:37

    The answer given above can't solve my problem.So I change async into false to get the alert message.

    jQuery.ajax({
                type:"post",
                dataType:"json",
                async: false,
                url: myAjax.ajaxurl,
                data: {action: 'submit_data', info: info},
                success: function(data) {
                    alert("Data was succesfully captured");
                },
            });
    
    0 讨论(0)
提交回复
热议问题