【发布时间】:2013-11-26 06:28:50
【问题描述】:
好的,所以我试图通过一个运行良好的插件在 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.");
})
})
});
返回“值已设置!”从这个函数;
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();
}
【问题讨论】:
-
不太清楚“但是下面的这个 Javascript 不会回到原始页面”是什么意思
-
同意@Ramesh。 Ajax 不应该重定向或转到另一个页面 - 它是出于完全相反的原因创建的 - 保持在当前页面而不刷新等。
-
啊,是的,我其实不希望它离开原来的页面! :) 也许这是一个 wordpress 的东西......但我正在尝试点击一个链接来更新用户的元数据,而无需离开页面......
标签: javascript jquery ajax wordpress refresh