Call Laravel 4 method using Angularjs

Asked

Viewed 136 times

1

I have the following heading structure at the angle:

.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 very well, locates the html files according to what is passed in the URL

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

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

In the User I have the following method:

public function getDados(){

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

In my angular 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();

Only when I call this angular method, it gives me a 404 error

Any hint of what might be happening?

  • What would that be /dados in his $http.get('api/usuarios/dados'), would not be get-dados?

  • If you are calling a json output, it has no purpose to load an html.

  • So, but I don’t want to call an html, I want to get this JSON just

  • when you send this url manually, what happens? api/users/data

  • I get this error: Not Found The requested URL /Laravel/api/users/get-data was not found on this server.

  • so the problem is that there is no such route. But what about this one? /Standard/api/users/data

  • take a look here: http://laravel.com/docs/5.1/testing#testing-json-Apis

  • So, give me the same error

  • Just to top it off, I use Laravel 4

  • Dude, the exit route to json is in the Aravel.

  • http://laravel.com/docs/4.2/eloquent#Converting-to-arrays-or-json Response:json($someArray) Return $model->toJson(); you don’t have to set anything at the angle unless you are doing Restful output of the js angular api.

  • http://fdietz.github.io/recipes-with-angular-js/consuming-external-services/consuming-restful-apis.html

  • When so, you will have a whole rule of use. https://docs.angularjs.org/api/ngResource/service/$Resource

  • Execute the command php artisan route:list at the root of your project and check that the "api/users/data" route is in the list.

Show 9 more comments

1 answer

0

Will give 404 error even if you are not called that url from the main page.

When you use api/usuarios/dados, you are defined as making a relative call. This may be the problem. If you are 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 putting /api/usuarios/dados, to fix the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.