Hello Guys,
I'm trying to figure out how to get the response from JSON into my Session::
Here's my in statement in blade
@if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
@endif
Here's the ajax request
<script>
$( document ).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$( "#status" ).change(function() {
var status = $('#status').prop('checked');
if(status == true){
var statusValue = 1;
}else{
statusValue = 0;
}
let _token = $('meta[name="csrf-token"]').attr('content');
//alert(statusValue);
$.ajax({
type:'POST',
url:"{{ route('statusRequest.post') }}",
data:{id:2, status:statusValue},
success:function(response){
console.log(response);
if(response) {
$('.success').text(response.success);
}
},
});
}); // End Status change
}); // End Doc ready
</script>
Here's the controller
class AjaxController extends Controller
{
public function updateStatus(Request $request)
{
$id = $request->get('id');
$application = Application::find($id);
$application->status = $request->get('status');
$application->save();
// Set up JSON respons for success message
if($application->status == 1){
$message = "Enabled";
}else{
$message = "Disabled";
}
return response()->json(['success'=>"Component has been $message"]);
}
}
Finally, here is the JSON response I get back in the console
{success: "Component has been Disabled"} or {success: "Component has been Enabled"}
Basically I'm trying to get it to flash a message when the values changes.
Thanks in Advance!