最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Wordpress AJAX doesn't work - response 0 - Stack Overflow

programmeradmin0浏览0评论

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.

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(); 
}
}

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() .

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);
?>

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.

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(); 
}
}

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() .

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);
?>
Share Improve this question edited Oct 26, 2014 at 18:11 kris_IV asked Oct 21, 2014 at 18:34 kris_IVkris_IV 2,4442 gold badges25 silver badges43 bronze badges 2
  • If you look at the source for the page is the jquery.js file being included? It should be, but if not you'll need to enqueue it. Do you see your JS in the source as well? – Arielle Lewis Commented Oct 21, 2014 at 18:41
  • Yes: Remote Address:[::1]:80 Request URL:localhost/wordpress/wp-includes/js/jquery/jquery.js?ver=1.11.0 For example when I add this line in jQuery(documet) script: $( "#secondary" ).append( "<p>Test</p>" ); It works perfect – kris_IV Commented Oct 21, 2014 at 18:45
Add a ment  | 

3 Answers 3

Reset to default 4

remove

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

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.

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

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' );

Just make sure that you have put the function 'fq_tag_support_callback()' in your plugin's main file.

I see few problems here. Action should inside the data object, not as a jQuery Ajax parameter. Also in the callback function data is stored in $_POST variable.

function test_callback() {

    $whatever = $_POST['whatever'];
    echo $whatever;

    die();
}
add_action('wp_ajax_nopriv_fqtag', 'test_callback');
add_action('wp_ajax_fqtag', 'test_callback');

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

评论列表(0)

  1. 暂无评论