Why plugin ajax response is -1?

I do not know why this response me -1 always? I am using this code in my plugin file.
i want response on click p tag

<?php    function myajax(){ ?>
        <script type="text/javascript">
        jQuery(document).ready(function(){
                jQuery("#testbutton").click(function(){
                     jQuery.ajax({
                        url: "<?php echo admin_url('admin-ajax.php'); ?>",
                        type: 'POST',
                        data: {
                        action: 'stravyfuncajax22'
                        },
                        dataType: 'html',
                        success: function(response) {
                        alert(response);
                        }
                    });
                }); 

            });
    </script><?php
    echo'
        <p id="testbutton1">click</p>';
    }

    add_shortcode("my_ajax", "myajax");


    add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
    add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');

    function testfunc11() {
    echo "2";
    die();
    }
?>

EDIT : added php tag now….

Related posts

Leave a Reply

2 comments

  1. according to wordPress jedi, scribu, you can now enqueue scripts from inside the plugin handler.

    http://scribu.net/wordpress/conditional-script-loading-revisited.html

    you were echoing things and in shortcode handling you have to return. i ran into some issues getting the script to work by returning the script, so i moved it to an external file which definitely works.

    function myajax(){ 
    
        wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
    
        $data = array( 
            'ajaxurl' => admin_url('admin-ajax.php')
        );
    
        wp_localize_script( 'my-script', 'myscriptL10n', $data );
    
        $text = '<a id="testbutton">click</a>';
    
        return $text;
    }
    add_shortcode("my_ajax", "myajax");
    
    
    add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
    add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');
    
    function testfunc11() {
        echo "2";
        die();
    }
    

    and then the my-script.js

    jQuery(document).ready(function($){
        $("#testbutton").click(function(){
            $.ajax({
                url: myscriptL10n.ajaxurl,
                type: 'POST',
                data: {
                    action: 'stravyfuncajax22'
                },
                dataType: 'html',
                success: function(response) {
                    alert(response);
                }
            });
        }); 
    });
    

    hope that helps.

    EDIT 1
    i didn’t test this part ,but i think it should work for echoing a form value as the ajax response.

    `
    function myajax(){

        wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
    
        $data = array( 
            'ajaxurl' => admin_url('admin-ajax.php')
        );
    
        wp_localize_script( 'my-script', 'myscriptL10n', $data );
    
        ob_start(); ?>
    
        <form id="form">
        <input type="text" name="foo" value="bar"/>
        <a id="testbutton">click</a>';
        </form>
    
        <?php $text = ob_get_contents();
    
        return $text;
    }
    add_shortcode("my_ajax", "myajax");
    
    
    add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
    add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');
    
    function testfunc11() {
        $data = $_POST['data'];
        echo $data['foo'];
        die();
    }
    

    `

    new javascript

    jQuery(document).ready(function($){
        $("#testbutton").click(function(){
             data = $('#form').serialize();
            $.ajax({
                url: myscriptL10n.ajaxurl,
                type: 'POST',
                data: {
                    action: 'stravyfuncajax22',
                                    data: data
                },
                dataType: 'html',
                success: function(response) {
                    alert(response);
                }
            });
        }); 
    });
    
  2. Try JSON encoding your response before echoing it like so (don’t forget the header either!).

    function testfunc11() {
        $response = json_encode( array( 'code' => 2 ) );
        header( "Content-Type: application/json" );
        echo $response;
        exit;
    }
    

    And then up in your JS code..

    alert( response.code );
    

    And it should show you ‘2’.

    However, I wouldn’t recommend using jQuery.ajax, instead I would recommend using this method which is (in my opinion) much easier to read and use.