Call Laravel method 4 using Angularjs

1

I have the following angular route structure:

.when('/', {
    redirectTo: '/pages/signin'
})
 .when('/:page', { // we can enable ngAnimate and implement the fix here, but it's a bit laggy
    templateUrl: function($routeParams) {
        return 'app/views/pages/'+ $routeParams.page +'.html';
    },
    resolve: function($routeParams) {
        return {deps: app.resolveScriptDeps(['js/controller.'+$routeParams.page+'.js'])};
    },              
    controller: 'Dash'
})

It works great, finds the html files according to what is in the URL

In my route.php file, I have this route:

Route::controller('api/usuarios', 'UsuariosController');

In UsersController I have the following method:

public function getDados(){

    return Response::json([
        'id' => '1',
        'texto' => 'TESTE TESTE ETSTE'
    ]);
}

In my angle controller, I have this method:

    $scope.getDados = function(){
    $http.get('api/usuarios/dados').
    success(function(data, status, headers, config) {
        console.log('DATA', data); 
    }).
    error(function(data) {

    });
}

$scope.getDados();

But when I call this angle method, it gives me a 404 error

Any tips on what might be happening?

    
asked by anonymous 18.08.2015 / 18:38

1 answer

0

It will give 404 error even if you are not called this url from the main page.

When you use api/usuarios/dados , you are set to make a relative call. This may be the problem. If it is on a page that contains meusite.com/usuarios in the url, the ajax call would be meusite.com.br/usuarios/api/usuarios/dados because the url is relative.

You can try to put /api/usuarios/dados to fix the problem.

    
14.12.2016 / 17:47