5

I'm trying to call a post from a Wordpress install using WP REST API. I keep getting an error in return and I can't find out why. Do I need to authenticate? One website said I don't need to authenticate for a GET request. WP REST API: Version 2.0-beta9. Here is the code I am using:

$(document).ready(function () {

setTimeout(GetPosts, 2000);

function GetPosts() {

     $.ajax({
        url: 'http://easterly.haamap.com/wp-json/posts',
        data: {
            filter: {
            'posts_per_page': 1
            }
        },
        dataType: 'json',
        type: 'GET',
        success: function(data) {
            console.log(data);
        },
        error: function() {
            console.log('error');
        }
    });

}

})

1 Answer 1

4

What version of the REST API are you using? If you're using the version that's bundled with WordPress 4.4 (ie, v2), you'll need to change your url to something like http://example.com/wp-json/wp/v2/posts.

Reference

WP API version 2 docs

11
  • I've tried that I still get the error. Is there a way to retrieve the error code?
    – The Hawk
    Commented Dec 23, 2015 at 14:47
  • What happens if you use console.log( 'error' ); when you get an error? When I try to GET a bogus REST URL, it gives me back a JSON object along these lines: {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}} (which is basically a JSONified WP_Error object).
    – Pat J
    Commented Dec 23, 2015 at 14:54
  • yes, I get 'error'. The Network shows a 404 for that url.
    – The Hawk
    Commented Dec 23, 2015 at 15:14
  • Can you please edit your question to indicate what version of the REST API you're using (v1, v2, or the version bundled with WordPress 4.4)? Thanks.
    – Pat J
    Commented Dec 23, 2015 at 15:17
  • 1
    This is the updated WP REST API V2 reference.
    – Brethlosze
    Commented Jan 13, 2019 at 1:08

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.