0

I'm trying to get a basic query going with the new V2 API and Angular in WordPress 4.4.1. Perhaps someone can help me understand why the URL, /wp-json/wp/v2/posts, gives a JSON response with a 404.

Browsing to that URL gives me JSON like this:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

And here is the JavaScript I'm using to make that .GET

var base    = 'http://localhost:8888/recruitler';
var posts   = '/wp-json/wp/v2/posts';
var user    = '/wp-json/wp/v2/users/'; // append user id
var s       = '/wp-json/wp/v2/posts?filter[s]='; // append search term

// basic HTTP call with Angular
var app = angular.module('app', ['ngRoute'])

app.factory('myService', function($http) {
    var myService = {
        async: function() {
          // $http returns a promise, which has a then function, which also returns a promise
          var promise = $http.get( base+posts ).then(function (response) {
            // The then function here is an opportunity to modify the response
            console.log(response);
            // The return value gets picked up by the then in the controller.
            return response.data;
          });
          // Return the promise to the controller
          return promise;
        }
    };
  return myService;
});

app.controller('MainCtrl', function( myService, $scope ) {
  // Call the async method and then do stuff with what is returned inside our own then function
    myService.async().then(function(d) {
        $scope.data = d;
    });
});

UPDATE: This must be a local environment issue. Live websites work just fine. I've tested and this issue persists on all my local WAMP and MAMP dev sites. Restarting the server and or checking this answer got it working.

1 Answer 1

1

the factory looks right, according to the rest-api docs you need pretty permalinks plugin as well in order to rest-api plugin use custom url rewrites https://wordpress.org/plugins/rest-api/installation/

Sign up to request clarification or add additional context in comments.

4 Comments

Oh wow you need that plugin as well? The API is kind of a mess huh? Now I'm getting GET http://localhost:8888/recruitler/wp-json/wp/v2/posts net::ERR_EMPTY_RESPONSE
try with 127.0.0.1 instead of localhost
I've been playing with the URL and researching for 4 hours now... I have no idea how to use the API.
I just installed wordpress on my laptop, I enabled numeric permalinks and added a sample .htaccess from codex.wordpress.org/htaccess (I used the Subfolder Example). If you have your wp installation in a subfolder you new to change the RewriteBase value. On my .htaccess it looks like this: RewriteBase /wordpress/

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.