AJAX调用不显示原始页面或刷新它

AJAX Call not showing original page or refreshing it

本文关键字:刷新 原始 调用 显示 AJAX      更新时间:2023-10-19

好的,所以我试图通过一个运行良好的插件在Wordpress中传递AJAX调用,但在数据传递给下面的函数后,下面的Javascript不会返回到原始页面。我错过了什么?

// JavaScript Document
jQuery(document).ready(function($) {
// We'll pass this variable to the PHP function outofoffice_ajax_request
id_numbers = new Array();
// This does the ajax request
$.ajax({
    type: "get",        
    url: myAjax.ajaxurl,
    dataType : "json",
    data: {         
        action:"outofoffice_ajax_request",
        outofoffice_value : outofoffice_value,
        aid : aid
    },
    success:function(response){
        // This outputs the result of the ajax request
        id_numbers = response;
        $("#outofoffice").html(response);   
                    window.location.reload(true);
    }       
    .error(function() {
alert("Your out of office settings could not be updated.");
})      
})
});

返回此函数的"value is set!";

function outofoffice_ajax_request() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST['outofoffice_value']) ) {
    $outofoffice = $_REQUEST['outofoffice_value'];
    $aid = $_REQUEST['aid'];
    if($_REQUEST['outofoffice_value']=='true'){
    update_user_meta($aid, 'si_office_status', 'false');  
    }
    else{
    update_user_meta($aid, 'si_office_status', 'true');     
    }
    echo "value is set!";
    die();
} 
die();   
}

没有真正需要进行

 if(response.type == "success"){

块,因为这已经由正在到达的成功回调决定了,如果发生错误,它不会到达该块,也不会像你希望的那样进入你的if语句,相反,你应该同时有一个成功回调和一个错误回调,如下所示:

success: function(data) {
    // Your success here
},
error: function() {
    // Your error handler here
}

这可能就是你遇到麻烦的原因。