【发布时间】:2016-04-14 10:03:38
【问题描述】:
我正在为站点中完成的所有操作制作一些日志,可以存储在 .txt 文件中。
我的 php 代码在正常加载时工作正常,但是当我使用 ajax 时,似乎我没有阅读 php 上的 fopen、fpost 方法..
请检查我的代码。
这是我的函数和 ajax 处理程序
function action_log( $msg ) {
$current_user = wp_get_current_user();
$path = "wp-content/themes/creation/templates/inc/log.txt";
$myfile = (file_exists($path)) ? fopen($path, "a+") : fopen($path, "w+");
$txt = $msg.$current_user->display_name."\n";
if ( $myfile ) {
fwrite( $myfile, $txt );
fclose( $myfile );
chmod($path, 0777);
$result = $txt;
return $result;
} else {
$result = $myfile;
return $txt;
}
}
add_action('wp_ajax_action-log-ajax', 'action_log_ajax');
add_action('wp_ajax_nopriv_action-log-ajax', 'action_log_ajax');
function action_log_ajax(){
$post = $_REQUEST;
$msg = $post['msg'];
$write = action_log( $msg );
exit( wp_send_json_success( $write ) );
}
我的 jquery 插件功能
$.send_log_changes = function( $msg ) {
var msgs = $msg;
var action = 'action-log-ajax';
console.log(msgs);
$.post({
url: plugins_ajax.ajaxUrl,
method: 'POST',
data: {
action: action,
msg: msgs
},
dataType: 'json',
success: function( respones ) {
console.log(respones);
}
});
}
我尝试使用 .ajax、.get、.post 仍然是同样的错误。
它总是返回False并且不能在文件中写入/打开。
【问题讨论】:
标签: php jquery ajax wordpress fopen