I keep getting an error when submitting form using Ajax post 500 (Internal Server Error). I am using angularjs to pass the form data and when it sends the data to my controller it going to using ajax to access the route and send the data to my controller but it keeps giving me an error. Here is my code
CommentController.php
public function store() {
Comment::create(array(
'author' => Input::get('author'),
'text' => Input::get('text')
));
return Response::json(array('success' => true));
}
Routes.php
Route::get('/', function () {
return view('app');
});
Route::group(['prefix' => 'api'], function() {
Route::get('comments', 'CommentController@index');
Route::post('comments', 'CommentController@store');
Route::delete('comments{comments}', 'CommentsController@destroy');
});
Comment.php
protected $fillable = [
'text',
'author'
];
mainCtrl.js
(function () {
'use strict';
angular.module('mainCtrl', [])
.controller('mainController', function($scope, $http, Comment) {
$scope.commentData = [];
$scope.loading = true;
Comment.get().success(function(data) {
$scope.comments = data;
$scope.loading = false;
});
$scope.submitComment = function() {
$scope.loading = true;
Comment.save($scope.commentData).success(function(data) {
Comment.get().success(function(getData) {
$scope.comments = getData;
$scope.loading = false;
});
}).error(function(data) {
console.log(data);
});
};
$scope.deleteComment = function(id) {
$scope.loading = true;
Comment.destroy(id)
.success(function(data) {
Comment.get()
.success(function(getData) {
$scope.comments = getData;
$scope.loading = false;
});
});
};
});
})();
commentService.js
(function () {
'use strict';
angular.module('commentService', []).factory('Comment', function($http) {
return {
get : function() {
return $http.get('/api/comments/');
},
save : function(commentData) {
return $http({
method: 'POST',
url: '/api/comments/',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: $.param(commentData)
});
},
destroy : function(id) {
return $http.delete('/api/comments/' + id);
}
}
});
})();
app.js
(function () {
'use strict';
var commentApp = angular.module('commentApp', ['mainCtrl', 'commentService']);
})();
app.blade.php
<form ng-submit="submitComment()">
<div class="form-group">
<input type="text" value="{{ csrf_token() }}" class="form-control input-sm" name="author" ng-model="commentData.author" placeholder="Name">
</div>
<div class="form-group">
<input type="text" value="{{ csrf_token() }}" class="form-control input-lg" name="text" ng-model="commentData.text" placeholder="Say what you have to say">
</div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</form>
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community