Manuel J
Manuel J

Reputation: 59

Laravel 4 can't get data from Angular Ajax

I am trying to develop my application in Laravel 4 and Angular JS, my application allows user to retrieve their Information through the system via Text Change.

Angular is used to pass data input from the user to Laravel which in turn retrieves the Information from the Database.

However Laravel is unable to retrieve the data passed from Angular.

View

<div data-ng-controller="ReservationController">
<input id='ERI' type='text' data-ng-model="scanRID" data-ng-change="queryRes()" name='exampleInput' maxlength='3' />
</div>

Angular Factory

app.factory('exampleFactory', function($http) {
    var factory = {};
    factory.getExample = function(scanRID) {
        return $http({
            method: 'GET',
            url: LARAVEL_CONTROLLER + 'Example',
            data: $.param(scanRID)
        });
    };
    return factory;
});

Angular Controller

app.controller('exampleController', function($scope, $http, exampleFactory) {
    $scope.queryRes = function() {
        if($scope.scanRID.length == 3) {
            exampleFactory.getExample($scope.scanRID)
                .success(function (data) {
                // Do Something Here
                })
                .error(function (data) {
                    console.log(data);
                });
        }
    };
});

Laravel 4 Routes

Route::get('Example', 'ExampleController@show');

Laravel 4 ExampleController

class ExampleController extends \BaseController {

    public function show()
    {

        $id = Input::get('scanRID'); // This here might be wrong. It's always empty!

        $data = ExampleModel::find($id); // Able to query and retrieve.

        return Response::JSON($data); // Returns empty.
    }

}

Laravel 4 ExampleModel

class ExampleModel extends Eloquent {

    // The id of this table is what I want, but I can't retrieve it.
    protected $fillable = ['id', 'ExampleData1', 'ExampleData2'];

    protected $table = 'exampleTable';
}

I have searched everywhere for a solution, it seems that everyone is able to successfully make the Ajax call. I think there is something that I am missing out that I am unaware about.

I have also tried setting CSRF Token, but however, I do not think that is an issue. So my last resort is to turn to the experts and hope someone is able to help me.

On a side note, I am fairly new to Laravel and Angular, so if you do post a solution, please explain to me the issue as I would like to learn more about Angular and Laravel.

Thank you for reviewing my issue.

Upvotes: 0

Views: 385

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

You are not passing the value of scanRID by scanRID parameter instead pass only the value without parameter. So you are try to get the value from scanRID using Input::get('scanRID'); but without having scanRID parameter. that should be the case ur not getting the value :)

 return $http({
        method: 'GET',
        url: LARAVEL_CONTROLLER + 'Example',
        data: $.param({scanRID:scanRID})               //Change Here
    });

OR

return $http({
    method: "GET",
    url: LARAVEL_CONTROLLER + 'Example',
    params: {scanRID:scanRID}                            //Change Here
);

change like this

Upvotes: 1

Related Questions