2012-08-07 48 views
2

現在已經查看了WP ajax文檔並且仍然不能 計算出來。我正在開發一個插件,這是爲了更新它的 選項,而不必刷新頁面。我已經設法通過wp-load完成它 ,但知道這是不好的做法,並希望做到正確。WordPress WP_ajax不能正常工作

我將把javascript移到一個單獨的.js文件中,一旦我擁有了所有的東西 就可以工作了。

所有的代碼都在單頁上。試圖通過ajax 更新一些選項,它只是不工作。響應說它成功了,但是 current_form選項沒有被更新。任何幫助將不勝感激。

代碼已經更新爲:

wp_enqueue_script('AWNT_save_form_data', plugin_dir_url(__FILE__) . 'includes/save_form_data.js', array('jquery')); 

wp_localize_script('AWNT_save_form_data', 'MyAjax', array(
    // URL to wp-admin/admin-ajax.php to process the request 
    'ajaxurl'   => admin_url('admin-ajax.php'), 

    // generate a nonce with a unique ID "myajax-post-comment-nonce" 
    // so that you can check it later when an AJAX request is sent 
    'postCommentNonce' => wp_create_nonce('myajax-post-comment-nonce'), 
    ) 
); 

add_action('wp_ajax_AWNT_save', 'AWNT_save_callback'); 

function AWNT_save_callback() { 
update_option('current_form', '5'); 
$nonce = $_POST['postCommentNonce']; 
if (! wp_verify_nonce($nonce, 'myajax-post-comment-nonce')) 
    die ('Busted!'); 
update_option('current_form', 'foo'); 
echo get_option('current_form'); 
die(); 
} 

JS文件(save_form_data.js):

被添加
jQuery(document).ready(function($) { 
$('#save').click(function() { 
     var data = { 
      action: 'AWNT_save', 
      postCommentNonce : MyAjax.postCommentNonce, 
      form_name : $('#form_name').val(), 
customC: $('#customC').is(":checked"), 
no_throttle: $('#no_throttle').is(":checked"), 
form_code : $('#form_code').val()}; 

     jQuery.post(MyAjax.ajaxurl, data, function(response) { 
      alert('Response: ' + response); 
     }); 
    }); 
}); 

腳本,看到的0響應警報,但update_option要麼沒有被調用或不工作。 current_form保持不變。

回答

0

你應該閱讀http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

首先分開的JavaScript文件,並使用wp_enqueue_script添加它。 然後使用wp_localize_script將nonce和ajaxurl傳遞到您的JavaScript文件。

在function.php

wp_localize_script('your-js-file', 'MyAjax', array(
    // URL to wp-admin/admin-ajax.php to process the request 
    'ajaxurl'   => admin_url('admin-ajax.php'), 

    // generate a nonce with a unique ID "myajax-post-comment-nonce" 
    // so that you can check it later when an AJAX request is sent 
    'postCommentNonce' => wp_create_nonce('myajax-post-comment-nonce'), 
    ) 
); 


// if both logged in and not logged in users can send this AJAX request, 
// add both of these actions, otherwise add only the appropriate one 
add_action('wp_ajax_AWNT_save', 'AWNT_save_callback'); 
add_action('wp_ajax_nopriv_AWNT_save', 'AWNT_save_callback'); 

function AWNT_save_callback() { 

//CHeck nonce FIRST HERE 

$nonce = $_POST['postCommentNonce']; 

// check to see if the submitted nonce matches with the 
// generated nonce we created earlier 
if (! wp_verify_nonce($nonce, 'myajax-post-comment-nonce')) 
    die ('Busted!') 

update_option('current_form', 'foo'); 
echo get_option('current_form'); 

die(); 
} 

在你的JavaScript文件

jQuery(document).ready(function($) { 
$('#save').click(function() { 
     var data = { 
      action: 'AWNT_save', 
      postCommentNonce : MyAjax.postCommentNonce, 
      form_name : $('#form_name').val(), 
customC: $('#customC').is(":checked"), 
no_throttle: $('#no_throttle').is(":checked"), 
form_code : $('#form_code').val()}; 

     jQuery.post(MyAjax.ajaxurl, data, function(response) { 
      alert('Response: ' + response); 
     }); 
    }); 
}); 
+0

謝謝,但似乎仍然沒有工作。更新了原始問題。 – 2012-08-07 18:23:39

+0

對不起,錯誤的答案。尋找add_action('wp_ajax_nopriv,看看它是否解決了我看到的問題 – chifliiiii 2012-08-07 19:01:51

+0

:)不要認爲它應該,它只適用於那些有管理權限的插件選項頁面。只是現在試了一下,仍然沒有。儘管腳本確實在運行,但所有的值都是正確的,並且它保持了響應的成功。但仍不會使用回調中的update_option行更新選項。 – 2012-08-07 19:05:42

-1

這應該是

add_action('wp_ajax_AWNT_save_callback', 'AWNT_save_callback'); 
add_action('wp_ajax_nopriv_AWNT_save_callback', 'AWNT_save_callback'); 

我的意思是掛鉤的名字是錯的。