Wordpress AJAX不起作用-响应0 [英] Wordpress AJAX doesn't work - response 0

查看:68
本文介绍了Wordpress AJAX不起作用-响应0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的插件添加AJAX支持,但是我对这个简单的东西有很大的疑问. WordPress不允许我使用普通的AJAX,我需要使用WordPress版本.

I want add AJAX support to my plugin and I have huge problem with this simple thing. WordPress isn't permitting me to use normal AJAX and I need to use WordPress version.

在任何时候,WordPress函数(应该生成输出)都会返回0.我认为原因是WP不会触发函数".我试图强制该功能多次运行,但是我不知道我可以改进什么.

At all times, the WordPress function (that should generate output) returns 0. And I think that the reason is that WP doesn't trigger 'function'. I try to force the function to run many times, but I don't have any idea what I can improve.

<?php
public function widget( $args, $instance ) {

$options = get_option('Free_Quotation_options');
?>
<script type="text/javascript" >

    jQuery(document).ready(function($) {        
        var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";

        jQuery.ajax({
            url: ajaxurl,
            type: 'POST',
            action: 'fqtag',
            data: {
                'whatever': 'text'
            },
            success: function (output) {
                $('#secondary').append(output);
            }       
        });
    });

</script> 
<?php
add_action( 'wp_ajax_fqtag', 'fqtag' );
add_action( 'wp_ajax_nopriv_fqtag', 'fqtag' );

function fqtag() {
    global $wpdb; 

    echo 'echo';

    die(); 
}
}

我尝试添加alert('echo');测试功能,但没有任何效果.我认为AJAX无法运行适当的功能:fq_tag_support_callback().

I try to add alert('echo'); to the test function, but it doesn't have any effect. I think that AJAX doesn't run the proper function: fq_tag_support_callback() .

在一开始,我对ajaxurl变量有疑问.未定义.这不是正常情况.我尝试使用以下方法解决此问题:

On the beginning I had a problem with ajaxurl variable. It was not defined. It's not a normal situation. I attempt to resolve this problem by using:

var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";

您有什么想法,我该如何解决这个问题?

Do you have any idea, how can I try to solve this problem?

---编辑---与大卫讨论后,我得到了这样的文件(所有时间都不起作用)

---EDIT--- After discussion with David I have file like this (all time doesn't work)

<?php
/*
    Plugin Name: TEST PLUGIN
    Description: TEST
    Author: Krzysztof Kubiak
    Version: 1.0
*/
function Test_01_settings_init(){
    register_setting( 'Test_01_settings_filed', 'Test_01_options', 'Test_01_validate' );
}
add_action('admin_init', 'Test_01_settings_init' );

function T01_init_method() {         
    wp_enqueue_script('jquery');      
}   
add_action('init', 'T01_init_method');

function Test_01_menu_page(){
    add_menu_page( 'Test_01', 'Test_01', 'manage_options', 'T01_menu_page', 'T01_add_page' );
    echo my_test();
}
add_action('admin_menu', 'Test_01_menu_page');

function my_test(){
    echo 'Function test';
}
function T01_add_page() {
    echo 'TEST_01_plugin';
}

function Test_01_validate($input) {
}   

//AJAX FROM HIRE

function test_callback() {
    $whatever = 8;
    echo $whatever;
    die();
}       
add_action( 'wp_ajax_nopriv_fqtag', 'test_callback', 1 );
add_action( 'wp_ajax_fqtag', 'test_callback', 1 );

function print_js() { ?>
    <script type="text/javascript">
    jQuery.ajax({
        url: 'wp-admin/admin-ajax.php',
        type: 'POST',
        action: 'fqtag',
        data: {
            'whatever': 'text'
        },
        success: function (output) {
          alert(output);
        }       
    });
    </script>
<?php
}
add_action('wp_print_footer_scripts', 'print_js', 1000);
?>

推荐答案

删除

<script>alert('echo');</script>

如果您检查控制台,您的响应应该是echo.我怀疑以上所有代码都在您的插件功能文件中.基本上,php函数应放在函数文件中.

your response should be echo if you check your console. I suspect all the above code is in your plugin functions file. Basically the php function should be placed in the functions file.

应该将jquery放在您要从中接收响应的模板中.

The jquery should be placed in the template from which you want to receive the response.

将其放在函数文件中...从类中删除jquery ...

Place this in your functions file...remove the jquery from the class...

add_action('wp_print_footer_scripts', 'print_js', 1000);

    function print_js() { ?>
    <script type="text/javascript">
    jQuery(document).ready(function(){

        jQuery.ajax({
            url: 'wp-admin/admin-ajax.php',
            type: 'POST',
            data: {
                'action': 'test_callback',
                'whatever': 'text'
            },
            success: function (output) {
              alert(output);
            }       
        }); 

    });
    </script>
<?php
}

将其移到班级之外...

Move this outside your class...

 function test_callback() {
                    $whatever = 8;
                    echo $whatever;
                    die();
 }

 add_action( 'wp_ajax_nopriv_testaction', 'test_callback' );
 add_action( 'wp_ajax_testaction', 'test_callback' );

这篇关于Wordpress AJAX不起作用-响应0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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