2016-07-21 10 views
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(); 
} 

は私がinitno_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); 
     } 

    }); 

ファイル?

+1

以下のように代わりにajaxobject.ajaxurl

のajaxurl使用する必要があり、単に素晴らしいajaxurl –

+1

@MujeebuRahmanを使用し、多くの感謝!あなたは私の一日を作った。 – XiLab

答えて

1

あなたはちょうどあなたがajaxobjectを必要とyの

jQuery.ajax({ 
     method: 'post', 
     url : ajaxurl, 
     dataType: "json", 
関連する問題