【发布时间】:2016-03-02 13:12:33
【问题描述】:
在我的 WordPress 插件的管理页面中,我有一个对 PHP 文件进行 ajax 调用的函数。如果一切顺利,这个 PHP 文件应该会更新 WordPress 中的选项。但是由于某种原因,当我尝试使用 update_option 时它不起作用。
这是我的activate.php:
if(isset($_GET['activate']) && $_GET['activate'] == "true") {
if(isset($_REQUEST['txtAC']) && isset($_REQUEST['txtKey'])) {
$ac = $_REQUEST['txtAC'];
$key = $_REQUEST['txtKey'];
if($ac != "" && $key != "") {
$api_url = "http://192.168.2.75/wouter/yii2/basic/web/cars/activate/" . $ac . "/" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $api_url);
$result = curl_exec($ch);
curl_close($ch);
$count = json_decode($result);
if($count == 1){
update_option("ac", $ac);
update_option("auth_key", $key);
echo "success";
} else {
echo "failed";
}
return;
} else {
echo "notSet";
return;
}
}
return;
}
所以在更新选项之前一切都很顺利。当我把它们放进去时,什么也没有发生,而且出现了服务器错误。
我做错了什么?
编辑
这是我的 jquery ajax 代码:
function activatePlugin() {
jQuery("#acError").html("");
jQuery("#keyError").html("");
var txtAC = document.getElementById("txtAC").value;
var txtKey = document.getElementById("txtKey").value;
if(txtAC == "") {
jQuery("#acError").html("Vul een klantnummer in");
}
if(txtKey == "") {
jQuery("#keyError").html("Vul een key in");
}
if(txtAC != "" && txtKey != "") {
jQuery.ajax({
url: "../wp-content/plugins/autocommerce/admin/activatePlugin.php?activate=true",
method: "POST",
data: { txtAC : txtAC, txtKey: txtKey }
}).done(function(msg) {
alert(msg);
if(msg == "success") {
location.reload();
} else if(msg == "failed") {
jQuery("#activateError").html("Gegevens onjuist. Controleer uw gegevens en probeer het opnieuw.");
} else if(msg == "notSet") {
jQuery("#activateError").html("Een of meerdere velden zijn onjuist ingevuld.");
} else {
alert(msg);
jQuery("#activateError").html("Er is een fout opgetreden. Probeer het later opnieuw.");
}
});
}
【问题讨论】:
-
你得到什么输出?
success、failed或notSet。当你说有一个服务器错误时,它是什么?它在php错误日志中吗? nginx/apache 错误日志? -
“并且出现服务器错误。” - 究竟是什么?
-
如果我删除 update_option。我会得到“成功”
-
update_option 是 WordPress 的一个功能
-
如果你在插件中使用Ajax,你应该follow the standard WordPress method;使用回调意味着您可以使用所有 WordPress 方法,而无需手动修改包括内容。