Php 添加未触发的动作钩子,因此Ajax返回未定义的
这是我第一次实现ajax。几天来,我一直在努力处理这个示例ajax代码,在stackoverflow中搜索,阅读博客,尝试了许多不同的解决方案,甚至发现了一些错误,但是。。代码仍然返回$response=Undefined/0 Wordpress在页面中上传ajax,页面通过ajax调用与服务器交互并返回成功,但是变量$response没有传递给jquery,response.status和response.message都没有 事实上,$response在Firefox控制台中未定义或为0 **在所有这些检查之后,我发布了这个问题。Snake_Eyes和Froxz发现了一些我纠正的错误。尽管如此,ajax仍然返回未定义的$response 我已经调查了Wordpress调用是否做了\u stuff\u pp()。 为此,我使用PHP函数error_log()逐行检查了do_stuff_pp()函数。分析表明do_stuff_pp()函数从未运行过。但我不知道为什么:(** 我使用的是jquery1.5.1、wp2.9.2和php5.2.6Php 添加未触发的动作钩子,因此Ajax返回未定义的,php,ajax,wordpress,jquery,Php,Ajax,Wordpress,Jquery,这是我第一次实现ajax。几天来,我一直在努力处理这个示例ajax代码,在stackoverflow中搜索,阅读博客,尝试了许多不同的解决方案,甚至发现了一些错误,但是。。代码仍然返回$response=Undefined/0 Wordpress在页面中上传ajax,页面通过ajax调用与服务器交互并返回成功,但是变量$response没有传递给jquery,response.status和response.message都没有 事实上,$response在Firefox控制台中未定义或为0 *
function my_init() {
switch ($page_num) {
case 4445: // enrollment page1
$path = get_bloginfo('template_directory');
$source = $path . "/js/ajax_test.js";
wp_enqueue_script(
$handle='this-is-my-ajax-handle',
$source,
$dependencies = array('jquery')
);
break;
}
switch ($page_num) {
case 4445: // enrollment page1
$data = array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajaxin_it'),
'action' => 'ajaxin_it'
);
wp_localize_script('this-is-my-ajax-handle','ajaxYall', $data);
break;
}
}
add_action('init', 'my_init');
// Define functions for ajax:
$my_action = 'ajaxin_it';
if (defined('DOING-AJAX') && DOING_AJAX) {
// for loggedin users
add_action('wp_ajax_' . $my_action, 'do_stuff_pp');
// for non logged in users
add_action('wp_ajax_nopriv_' . $my_action, 'do_stuff_pp');
}
function do_stuff_pp(){
$response = array(
'status' => 'error',
'message' => "It's AJAX Y'all!"
);
// check is a legitimate call
if(isset($_GET['nonce']) && wp_verify_nonce($_GET['nonce'], 'ajaxin_it')){
$response = array(
'status' => 'success',
'message' => "It's AJAX Y'all!"
);
}
header('Content: application/json');
echo json_encode($response);
die();
}
这是.js:
jQuery(document).ready(function($){
$('#text_ajax_yall').click(function(){
var $el = $(this);
alert(ajaxYall.nonce);
$.ajax({url:ajaxYall.ajaxurl,action:ajaxYall.action,nonce:ajaxYall.nonce,type:'GET',dataType:'json',success:function(response, textStatus, jqXHR){
if( 200 == jqXHR.status && 'success' == textStatus ) {
if( 'success' == response.status ){
$el.after( '<p style="color:green;">' + response.message+ '</p>' );
} else {
$el.after( '<p style="color:red;">' + response.message + '</p>' );
}
}
// $el.after( '<p style="color:green;">' + response.message + '</p>' );
console.log(response.message, textStatus, jqXHR );
}});
});
});
jQuery(document).ready(函数($){
$('#text_ajax_yall')。单击(函数(){
var$el=$(本);
警报(ajaxYall.nonce);
$.ajax({url:ajaxYall.ajaxurl,action:ajaxYall.action,nonce:ajaxYall.nonce,类型:'GET',数据类型:'json',成功:函数(响应,文本状态,jqXHR){
if(200==jqXHR.status&“success”==textStatus){
如果('success'==response.status){
$el.after(“”+response.message+”
”);
}否则{
$el.after(“”+response.message+”
”);
}
}
//$el.after(“”+response.message+”
”);
日志(response.message、textStatus、jqXHR);
}});
});
});
产生这种情况的具体原因是什么?我如何修复它?
response.prova
您尝试获取值,但正如我在php中看到的,您没有带有值prova的数组…尝试控制台。log
像这样响应
console.log(response);
在数组中有两个值“status”和“message”
所以基本上是
response.status和response.message
伙计,您的PHP代码中没有定义prova
属性
你有这样的想法:
$response = array(
'status' => 'error',
'message' => "It's AJAX Y'all!"
);
所以你必须打电话:
console.log(response.status);
或
我正在使用PHP 5.2.6。谢谢。更改了它,但有相同的问题。response返回0。response.status返回undefined。response.message返回undefined。我怀疑PHP没有执行do_stuff();没有检测到数据wan有更深层的问题。.nonce必须在数据对象{}中…谢谢。我已经更新了代码,问题依然存在。我已经逐行测试了php代码ajax函数,发现do_stuff()从未被触发。我将do_stuff的名称更改为其他名称,并使用了“ajaxin_it”而不是“ajaxin it”对于操作挂钩。我想知道ajax和wordpress之间是否存在任何类型的冲突..或者我可能错误地指定了ajax的add_操作..请提供帮助。有一个更深层次的问题解释了为什么数据未定义..nonce必须在数据对象{}中:)
console.log(response.message);