Answer the question
In order to leave comments, you need to log in
How to send data via Ajax in Laravel?
Hi all. Help me deal with Ajax on Laravel, I still can't send data.
ajax itself
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#recipe_comments').on('submit', function(e) {
e.preventDefault();
var text = $('.text').val();
$.ajax({
type: "POST",
url: '/add_comment',
data: {text:text},
success: function(param)
{
console.log(param);
},
error: function(msg){
console.log('error');
}
});
});
<form class="form-horizontal" method="POST" id="recipe_comments" enctype = "multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<textarea class="form-control text" rows="4" placeholder="Ваш отзыв"></textarea>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Route::post('/add_comment', '[email protected]');
class CommentsController extends Controller
{
public function addComment(Request $request) {
if($request->ajax()){
$comment = new Comment();
$comment->text = Input::get('text');
$comment->date = Carbon::now();
$comment->save();
$response = array(
'status' => 'success',
'msg' => 'Setting created successfully',
);
return Response::json($response);
return 'yes';
}else{
return 'no';
}
}
}
POST http://el-recipes/add_comment 500 (Internal Server Error)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question