this is JS code:
<script type="text/javascript">
$(document).ready(function(){
$('#add-more-acadimic').on('click', function(){
$('#add-new-acadimic').modal();
});
$('#SaveAcadimic').on('click', function(){
var name = $('#acadimic').val();
$.post("{{ route('postInsertAcadimic')}}",{name: name},function(data){
console.log(data);
});
});
});
</script>
and this is my route file
Route::post('/manage/course/insert_acadimics', [
'uses' => 'CourseController#postInsertAcadimic',
'as' => 'postInsertAcadimic'
]);
this is controller function
public function postInsertAcadimic(Request $request)
{
if($request->ajax())
{
return response(Academic::create($request->all()));
}
}
and i use csrf token meta tag along with this code in master blade:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}});
and this is the console error:
POST http://localhost:8000/manage/course/insert_acadimics 500 (Internal Server Error) XHR failed loading: POST "http://localhost:8000/manage/course/insert_acadimics".
and this is laravel log error:
[2017-08-06 19:32:42] local.ERROR: exception 'Symfony\Component\Console\Exception\RuntimeException' with message 'Too many arguments, expected arguments "command" "name".' in C:\xampp\htdocs\SMS\vendor\symfony\console\Input\ArgvInput.php:181
Related
i want to make notification in laravel using ajax and get data from controller but i don't know why this always said 500 internal server error
this is my ajax
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).ready(function(){
// updating the view with notifications using ajax
function load_unseen_notification(view = '')
{
$.ajax({
url:"notif",
method:"POST",
beforeSend: function(xhr){xhr.setRequestHeader('X-CSRF-TOKEN', $("#token").attr('content'));},
data:{view:view},
dataType:"json",
success:function(data)
{
$('.dropdown-menu').html(data.notification);
if(data.unseen_notification > 0)
{
$('.count').html(data.unseen_notification);
}
}
});
}
load_unseen_notification();
setInterval(function(){
load_unseen_notification();;
}, 5000);
});
controller
public function index()
{
$pengumuman = Pengumuman::select("pengumuman.*")->count('pengumuman.id');
$data = array(
'unseen_notification' => $pengumuman
);
}
web.php
Route::Post('notif', 'NotifController#index')->name('notif');
I am trying to activate my user by doing an AJAX call. I have this jQuery code for that:
$(document).ready(function(){
$(document).one('click','.continue-button',function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var id = $(this).data('id');
$.ajax({
url: '/activate',
type: 'POST',
data: {id : id},
success: function(res){
}
});
});
});
It takes the data-id attribute of my button, which is the user's id and sends an AJAX call. This is how my route looks like:
Route::post('/activate','ActiveController#activate');
My function:
public function activate(Request $request)
{
$id = $request->input('id');
User::where('id',$id)->update([
'active' => '1'
]);
return redirect('/loadDashboard');
}
It activates the user and then redirects to '/loadDashboard' route. This is how the route looks like:
Route::group( ['middleware' => 'auth' ], function()
{
Route::get('/loadDashboard','ActiveController#loadDashboard');
});
And finally my loadDashboard function:
public function loadDashboard()
{
return view('dashboard')->with('title','Dashboard');
}
I want to redirect the user to my dashboard view in aforementioned function, but it seems to return the view to my AJAX call. I can see the view in Inspect->Network. How can I fix this problem?
Instead of this
return redirect('/loadDashboard');
put this
return response()->json(['url'=>url('/loadDashboard')]);
and in your ajax success function put this:
success: function(res){
window.location=res.url;
}
app.jss file
$('#modal-save').on('click', function(){
// geting the properties
$.ajax({
method:'POST',
url: url,
data: {body: $('#post-body').val(), postId: postId , _token: token}
})
// after done
.done(function(msg) {
$(postBodyElement).text(msg['new_body']);
$('#edit-modal').modal('hide')
});
});
my script in view code
<script>
var token = '{{ Session::token() }}';
var url = '{{ route('edit') }}';
</script>
Route file
Route::post('/edit', [
'uses'=>'PostController#postEditPost',
'as'=>'edit'
]);
My controller file
public function postEditPost( Request $request)
{
$this->validate($request,[
'body' => 'required'
]);
// checking auth user
$post = Post::find($request['postId']);
if(Auth::user != $post->user){
return redirect()->back();
}
// updating the post content
$post->body = $request['body'];
$post->update();
return response()->json(['new_body' => $post->body], 200);
}
Your'e sending an ajax request to the server, but dont accept it like so in the controller.
Try this:
// updating the post content
$post->body = $request['body'];
$post->update();
if(request()->expectsJson()){
return response()->json(['new_body' => $post->body], 200);
}
//here you can return to whereever you need it if its not ajax
return redirect('/');
Because of the VerifyCsrfToken middleware, you must provide CSRF token with each post, put, delete request.
Add meta tag inside head tag
<meta name="csrf-token" content="{{ csrf_token() }}">
Then use it in you ajax request
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
See X-CSRF-TOKEN
I got this error from Ajax call ! it's get action from checkbox then send data by Ajax to controller method,
jquery.min.js:4 POST http://localhost:8000/listhotelregisration 500 (Internal Server Error)
Here's the code html part:
<div style="display:block">
<div>My hotel Lists</div>
#foreach($myLists as $key => $val)
{{ $val['name'] }
{!! Form::checkbox($val['name'], $val['id'].','.$value['id']) !!} <br>
#endforeach
</div>
Ajax part:
$(function() {
$("input[type='checkbox']").change(function() {
var smi = $(this).val();
// alert(smi);
$.ajax({
url: 'listhotelregisration',
type: "post",
data: {'checko':smi},
success: function(data){
//alert(data);
}
});
});
Route part:
Route::post('listhotelregisration', 'ListhotelController#create');
Controller part:
public function create(Request $request)
{
$listhotel = new listhotel;
$data = $request->all();
$dataPrim = explode(",", $data);
$listhotel->id_list= $dataPrim[0];
$listhotel->id_hotel= $dataPrim[1];
$listhotel->save();
$response = "ok";
return response ()->json ($response);
}
Ajax in Laravel 5
This is mainly due to The VerifyCSRFToken middleware that laravel provide us out-of-the-box. It responds an Internal Server Error (500) when token mismatch occur on our requests.
We need to send our AJAX request with token sent to our browser.
The CSRF token is mostly stored in the meta tag, you can put it any where though
$.ajaxSetup
Set default values for future Ajax requests
Solution
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
});
Hope this helps.
error 500 will mean this is a server error.
To know exactly what went wrong you need to check the response the server is returning.
In any case i would already adjust your following pieces:
Javascript:
$(function() {
$("input[type='checkbox']").change(function() {
var id_list = $(this).attr('id_list');
var id_hotel = $(this).attr('id_hotel');
$.ajax({
url: 'listhotelregisration',
type: "post",
data: {
'id_list': id_list,
'id_hotel':id_hotel
}
}
});
});
Controller:
public function create(Request $request)
{
$data = $request->only(['id_list', 'id_hotel']);
$listhotel = listhotel::firstOrCreate($data)
return response ()->json ("OK");
}
My issue is that I'm doing a simple AJAX Post to update a database record, but I'm not getting any error and it's still not updating the record. What am I overlooking?
Thanks!
JS:
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
if ($('.abc-status').css('display') == 'block')
{
abc.api({method: 'user'}, function(error, user) {
var userId = $('.oath_id').text();
var username = user.display_name;
console.log(username);
$.ajax({
type: "POST",
url: "oauth_authorization/abc/"+username,
data: {abc_username: username},
error: function(data){
console.log(data);
}
});
});
}
Controller:
public function postabcOAuth(Request $request, $username)
{
Auth::user()->update([
'abc_username' => $username,
]);
}
Route:
Route::post('/oauth_authorization/abc/{username}', [
'uses' => '\abc\Http\Controllers\OAuthController#postAbcOAuth',
'middleware' => ['auth'],
]);
You can change the post method in route path and also in ajax function
Using the following in the Controller, I was able to get it to work.
public function postAbcOAuth(Request $request, $username)
{
DB::table('users')
->where('id',Auth::user()->id)
->update(['abc_username' => $username]);
}