1

I'm making an ajax request from my primary domain to a subdomain. I've solved my cross origin issues so I'm not getting an error. The call is returning data:0. I've checked my response config and the ajax url is correct as well as "action". My functions.php looks like this:

add_action("wp_ajax_sendhire", "sendhire");
add_action("wp_ajax_nopriv_sendhire", "sendhire");

function sendhire() {

    $cars = array("Volvo", "BMW", "Toyota");
    return json_encode($cars);
    die();

}

The data, obviously, is just for testing.

1
  • echo doesn't change the response. The will eventually process data being passed to it not return anything. Maybe a success/error message but that's about it.
    – dcp3450
    Commented Jul 31, 2017 at 4:37

2 Answers 2

0

If you want to use Admin-Ajax to output the content, you should use echo instead. There is nothing to be viewed when you use return, so you'll get a 0.

This quotation from the codex explains further about the 0 response:

If the Ajax request fails in wp-admin/admin-ajax.php, the response will be -1 or 0, depending on the reason for the failure. Additionally, if the request succeeds, but the Ajax action does not match a WordPress hook defined with add_action('wp_ajax_(action)', ...) or add_action('wp_ajax_nopriv_(action)', ...), then admin-ajax.php will respond 0.

Now, if you are going to output a JSON response, you should take a look into the REST API. Its default response type is JSON.

To do so, register a path for the endpoint, and create a callback function:

add_action( 'rest_api_init', function () {
    register_rest_route( 'dcp3450', '/test_endpoint/', array(
            'methods' => 'GET', 
            'callback' => 'sendhire' 
    ) );
});

function sendhire() {

    $cars = array("Volvo", "BMW", "Toyota");
    return $cars;

}

Now by accessing http://example.com/wp-json/dcp3450/test_endpoint/ you will get your JSON response, and you can get rid of this annoying 0 that is following humanity to its end.

10
  • I'm using the REST API for the site currently to pull data using WP as a headless cms. I'm attempting to use the ajax call to process a form.
    – dcp3450
    Commented Jul 31, 2017 at 4:40
  • I've tried your code, and it works with echo, but outputs 0 with return. The Admin-Ajax must echo the content if you are trying to send it to the browser.
    – Johansson
    Commented Jul 31, 2017 at 4:43
  • I wonder if there is an issue with me making a call to a subdomain?
    – dcp3450
    Commented Jul 31, 2017 at 4:44
  • I can't tell since you didn't include any details about that. But to mention again, you will always get the 0 if you return the data, since there is nothing to be displayed.
    – Johansson
    Commented Jul 31, 2017 at 4:45
  • I changed return to echo but I still get 0.
    – dcp3450
    Commented Jul 31, 2017 at 4:46
0

WordPress have a very useful API (REST API) for this work, you can use it, instead of sending ajax request. Here is the documentation have a look please https://developer.wordpress.org/rest-api/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.