字preSS WP_ajax不工作 [英] WordPress WP_ajax not working

查看:126
本文介绍了字preSS WP_ajax不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经看了WP AJAX文档小时,现在仍不能 弄清楚了这一点。我工作的一个插件,这是更新它的 选项​​,而无需刷新页面。我已经成功地完成它 通过WP-载荷,但知道那是不好的做法,并希望做是正确的。

Have been looked at the WP ajax documentation for hours now and still can't figure this out. I am working on a plugin and this is for updating it's options without having to refresh the page. I've managed to accomplish it through wp-load, but know thats bad practice and would like to do it correctly.

我会动的JavaScript到一个单独的.js文件,一旦我拥有了一切 和工作。

I will be moving the javascript to a separate .js file, once I have everything up and working.

所有的code是在单页。尝试更新通过AJAX的一些选项 它只是不工作。响应是说其成功,但 current_form选项没有被更新。任何帮助将大大AP preciated。

All of the code is in on single page. Trying to update some options via ajax and it just is not working. The response is saying its successful, but the current_form option is not being updated. Any help would be greatly appreciated.

code现在已经更新为:

Code is now updated to:

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):

JS file (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保持相同。

Script is being added, see an alert for the response of 0, but the update_option either isn't being called or isn't working. current_form remains the same.

推荐答案

这应该是

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

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

I mean the hook name is wrong.

这篇关于字preSS WP_ajax不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆