2
Wordpress + Ajaxで作業していて、適切なフックを使用しても "ReferenceError:変数が見つかりません:ajaxobject"というエラーが表示されます。もちろん私のアヤックスルルにはいくつかの問題がありますが、それは私にはうまくいったように思えます。手伝って頂けますか?私のfunctions.php(Wordpress/Ajax)ReferenceError:変数を見つけることができません:ajaxobject
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はupdate_portfolio_function
が
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
使うべきのように見えるもちろんの
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);
}
});
ファイル?
以下のように代わりにajaxobject.ajaxurl
のajaxurl使用する必要があり、単に素晴らしいajaxurl –
@MujeebuRahmanを使用し、多くの感謝!あなたは私の一日を作った。 – XiLab