Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  86] [ 3]  / answers: 1 / hits: 22282  / 7 Years ago, fri, february 10, 2017, 12:00:00

I am new to Laravel and am using Laravel 5.3. I want to make a text field where it will automatically suggest some data and when I select a data it will add it to an array. I want to send that array to a controller for further use. For this the




view file is as follows:




<head>
<script src=http://code.jquery.com/jquery-1.10.2.js></script>
<script src=http://code.jquery.com/ui/1.11.4/jquery-ui.js></script>
<script>
$(document).ready(function() {
var members = {!! json_encode($member) !!};
console.log(members);
var arr = [];
$(#tags).autocomplete({
source: members,
select: function (event, ui) {
arr.push(ui);
console.log(arr);
}
});
$(#submit).click(function(event){
$.ajax({
type: POST,
url: '/storeresearch',
data: {selectedMembers: arr},
success: function( msg ) {
console.log(msg);
}
});
});
});
</script>
</head>
<body>
<form id=hu action=/storeresearch method=POST>
{!! csrf_field() !!}
<label>Research Author</label>
<input type=text id=tags name=researchsupervisor_1 value=>
<input type=submit name=submit id=submit class=btn btn-primary value=Add>
</form>
</body>



My Controller file is as follows:




public function store(Request $request){
if($request->ajax())
{
$mem = $request->all();
return response()->json($mem,200) ;
}
else{
return not found;
}



And web.php is as followings:




Route::post('/storeresearch','ResearchController@store');


But it seems that there is no ajax call happening. In the controller it always enters the else section. What is the problem can anyone help?


More From » php

 Answers
20

I solved this problem by doing following



$.ajax({
type:'POST',
url:'your url',
data:{_token: {{ csrf_token() }}
},
success: function( msg ) {
}
});

[#58992] Wednesday, February 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tye

Total Points: 415
Total Questions: 103
Total Answers: 116

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;