• Resolved haampie

    (@haampie)


    Hello there :]

    I’m trying to create a ajaxrequest in a plugin using jQuery and admin-ajax.php. But the response of this request always ends with a zero. I’m not sure it’s an error or a bug or so, but this is not how it should be anyway :]

    This is a part of the code:

    function secretnameofmyawesomeplugin_js(){
    	if(isset($_POST['url']) && !empty($_POST['url'])){
    		echo 'ok';
    	} else {
    		echo 'false';
    	}
    }

    // At this point the Javascript was loaded
    add_action('admin_menu', 'init');
    // The connection between admin-ajax and my php ajax function
    add_action('wp_ajax_secretnameofmyawesomeplugin_js', 'secretnameofmyawesomeplugin_js', 10);

    This is a piece of the Javascript code (obviously I’m using PHP also):

    $.post('<?php echo $site_url;?>/wp-admin/admin-ajax.php',
    	{
    		'action': 'secretnameofmyawesomeplugin_js',
    		'cookie': encodeURIComponent(document.cookie),
    		'url': url
    	}, function(str){
    		if(str == 'ok'){
    			alert('Ok!');
    		} else {
    			alert('Noooeess, something went wrong: ' + str);
    		}
    	}
    );

    The response is ok0 instead of ok at the moment. Does anyone know a solution to get rid of the 0 at the end?

Viewing 1 replies (of 1 total)
  • Thread Starter haampie

    (@haampie)

    Got it :]

    There is one line of script in admin-ajax.php after my own ajax_action that says: die('0');
    The solution is to add die() to the end of your own ajax function, so you die the script before die('0') :]

    function secretnameofmyawesomeplugin_js(){
    	if(isset($_POST['url']) && !empty($_POST['url'])){
    		echo 'ok';
    	} else {
    		echo 'false';
    	}
    }
    die()
Viewing 1 replies (of 1 total)
  • The topic ‘Always a zero after an ajax response’ is closed to new replies.