Javascript (Wordpress/Ajax)引用错误:Can';找不到变量:ajaxobject
我正在使用Wordpress+Ajax,甚至使用适当的钩子,我得到了错误“引用错误:找不到变量:ajaxobject”。当然,我的ajaxurl有一些问题,但我不知道在哪里,因为它对我来说似乎做得很好。你能帮我吗 在my functions.php中Javascript (Wordpress/Ajax)引用错误:Can';找不到变量:ajaxobject,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我正在使用Wordpress+Ajax,甚至使用适当的钩子,我得到了错误“引用错误:找不到变量:ajaxobject”。当然,我的ajaxurl有一些问题,但我不知道在哪里,因为它对我来说似乎做得很好。你能帮我吗 在my functions.php中 add_action( 'wp_enqueue_scripts', 'add_frontend_ajax_javascript_file', 11, 2 ); function add_frontend_ajax_javascript_file()
add_action( 'wp_enqueue_scripts', 'add_frontend_ajax_javascript_file', 11, 2 );
function add_frontend_ajax_javascript_file()
{
wp_localize_script( 'ajax-script', 'ajaxobject', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
我的jQuery/AJAX文件
var itemtitle = $('#itemtitle').val();
var itemdescription = $('#itemdescription').val();
jQuery.ajax({
method: 'post',
url : ajaxobject.ajaxurl, //Why???
dataType: "json",
data: {
'action':'update_portfolio_function',
'pid' : id,
'itemtitle' : itemtitle,
'itemdescription' : itemdescription,
},
success:function(data) {
// This outputs the result of the ajax request
alert("Coooool");
},
error: function(errorThrown){
console.log(errorThrown);
}
});
当然,update\u portfolio\u函数
add_action('wp_ajax_update_portfolio', 'update_portfolio_function' );
function update_portfolio_function(){
$id = $_REQUEST['pid'];
$title = $_REQUEST['itemtitle'];
$description = $_REQUEST['itemdescription'];
$attachment = array(
'ID' => $id,
'post_title' => $title,
'post_content' => $description
);
// now update main post body
wp_update_post( $attachment );
die();
}
我应该使用init
还是no_priv
?您只需要使用ajaxurl而不是ajaxobject.ajaxurl
如下
jQuery.ajax({
method: 'post',
url : ajaxurl,
dataType: "json",
如果需要ajaxobject,只需使用ajaxurl@MujeebuRahman太好了,非常感谢!你让我开心。