2012-08-07 46 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'); 

我的意思是挂钩的名字是错的。