Laravel 5.5: 403 Forbidden when making AJAX POST











up vote
1
down vote

favorite












I've been stuck on this for 2 days now, and all the resolutions I looked for on StackOverflow and Laracasts proved inconclusive.



I am using Laravel 5.5 with jQuery and I do my testing in Firefox.



My AJAX GET calls are working fine, however, when I try and push an entry in my database, I get a 403 error.



My header does contain the CSRF token:



<meta name="csrf-token" content="{{ csrf_token() }}">


Models are created for every table called in my controller:



public function pushProfile(Request $request){
$userid = Auth::user()->id;
$data = $request->message;
$stat = AppCharacter::where('owner', $userid)->first();
$mess = AppMessage::firstOrCreate([
'posterID' => $userid,
'loc_x' => '0',
'loc_y' => '0',
'characterID' => $stat->id,
'type' => 'profile'
]);
$mess->content = $data;
$mess->save();
return response()->json(['success'=>'Message has been saved!']);
}


Here is the AJAX call, it basically checks for my Quilljs Delta. This Deltais a JSON object formatting a message from WYSIWYG. Then every 5th second, it should try to push it to my database.



I know the Quilljs side works fine because my deltas show properly in my console. But the POST call itself doesn't seem to pass authentication for some reason? (This is just my guess, to me it seems like the only reason it would send a 403.)



setInterval(function() {
if (change.length() > 0) {
console.log('Saving changes', change);
/* AJAX setup */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
headers: {
'Content-Type':'application/json'
},
method: 'POST',
url: '{{ url("/pushProfile") }}',
data: {
message:
{
doc: JSON.stringify(quill.getContents())
},
_token: $('meta[name="csrf-token"]').attr('content')
},
dataType: 'JSON',
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
},
success: function (data) {
$(".writeinfo").append(data.msg);
console.log("Success!!!");
}
});
change = new Delta();
}
}, 5*1000);


To make sure the issue didn't come from CSRF, I went a bit overkill and, after trying to set up the token first in ajaxSetup, then in the AJAX data only, I just assigned it in both. None of these scenarios changed anything.



Of course, I assigned the 'Web' middleware on my post route to check for the above-mentioned CSRF token. The route I use is as follows:



Route::group(['middleware' => ['web']], function () {
Route::post('/pushProfile','MessageSend@pushProfile')->name('pushProfile');
});


I also tried to assign the URL as:



url: '/pushProfile',


To no avail, unfortunately... This just returns a 404 instead of the 403 I currently have:



{
"readyState":4,
"responseText":"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">n<html>
<head>n<title>403 Forbidden</title>n</head>
<body>n<h1>Forbidden</h1>n
<p>You don't have permission to access
/folder/public/{{ route(&quot;pushProfile&quot;) }}n
on this server.<br />n</p>n
<hr>n
<address>Apache/2.4.35 (Win64) PHP/7.2.10 Server at localhost Port 80</address>n
</body>
</html>n",
"status":403,
"statusText":"Forbidden"
}


Did I miss anything?
Thanks!










share|improve this question
























  • try url: '{{ route("pushProfile") }}',
    – madalinivascu
    Nov 7 at 13:26












  • Unfortunately, network monitoring still returns a 403 on url: '{{ route("pushProfile") }}', through my localhost. Thanks though :)
    – mbuss
    Nov 7 at 13:33












  • Does allow "post" method your route?
    – FGDeveloper
    Nov 7 at 14:26












  • My route is Route::post, and my controller calls "use IlluminateHttpRequest;". Or do you mean setting up Guards and Providers? I didn't see it as a requirement in any docs :/
    – mbuss
    Nov 8 at 9:42















up vote
1
down vote

favorite












I've been stuck on this for 2 days now, and all the resolutions I looked for on StackOverflow and Laracasts proved inconclusive.



I am using Laravel 5.5 with jQuery and I do my testing in Firefox.



My AJAX GET calls are working fine, however, when I try and push an entry in my database, I get a 403 error.



My header does contain the CSRF token:



<meta name="csrf-token" content="{{ csrf_token() }}">


Models are created for every table called in my controller:



public function pushProfile(Request $request){
$userid = Auth::user()->id;
$data = $request->message;
$stat = AppCharacter::where('owner', $userid)->first();
$mess = AppMessage::firstOrCreate([
'posterID' => $userid,
'loc_x' => '0',
'loc_y' => '0',
'characterID' => $stat->id,
'type' => 'profile'
]);
$mess->content = $data;
$mess->save();
return response()->json(['success'=>'Message has been saved!']);
}


Here is the AJAX call, it basically checks for my Quilljs Delta. This Deltais a JSON object formatting a message from WYSIWYG. Then every 5th second, it should try to push it to my database.



I know the Quilljs side works fine because my deltas show properly in my console. But the POST call itself doesn't seem to pass authentication for some reason? (This is just my guess, to me it seems like the only reason it would send a 403.)



setInterval(function() {
if (change.length() > 0) {
console.log('Saving changes', change);
/* AJAX setup */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
headers: {
'Content-Type':'application/json'
},
method: 'POST',
url: '{{ url("/pushProfile") }}',
data: {
message:
{
doc: JSON.stringify(quill.getContents())
},
_token: $('meta[name="csrf-token"]').attr('content')
},
dataType: 'JSON',
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
},
success: function (data) {
$(".writeinfo").append(data.msg);
console.log("Success!!!");
}
});
change = new Delta();
}
}, 5*1000);


To make sure the issue didn't come from CSRF, I went a bit overkill and, after trying to set up the token first in ajaxSetup, then in the AJAX data only, I just assigned it in both. None of these scenarios changed anything.



Of course, I assigned the 'Web' middleware on my post route to check for the above-mentioned CSRF token. The route I use is as follows:



Route::group(['middleware' => ['web']], function () {
Route::post('/pushProfile','MessageSend@pushProfile')->name('pushProfile');
});


I also tried to assign the URL as:



url: '/pushProfile',


To no avail, unfortunately... This just returns a 404 instead of the 403 I currently have:



{
"readyState":4,
"responseText":"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">n<html>
<head>n<title>403 Forbidden</title>n</head>
<body>n<h1>Forbidden</h1>n
<p>You don't have permission to access
/folder/public/{{ route(&quot;pushProfile&quot;) }}n
on this server.<br />n</p>n
<hr>n
<address>Apache/2.4.35 (Win64) PHP/7.2.10 Server at localhost Port 80</address>n
</body>
</html>n",
"status":403,
"statusText":"Forbidden"
}


Did I miss anything?
Thanks!










share|improve this question
























  • try url: '{{ route("pushProfile") }}',
    – madalinivascu
    Nov 7 at 13:26












  • Unfortunately, network monitoring still returns a 403 on url: '{{ route("pushProfile") }}', through my localhost. Thanks though :)
    – mbuss
    Nov 7 at 13:33












  • Does allow "post" method your route?
    – FGDeveloper
    Nov 7 at 14:26












  • My route is Route::post, and my controller calls "use IlluminateHttpRequest;". Or do you mean setting up Guards and Providers? I didn't see it as a requirement in any docs :/
    – mbuss
    Nov 8 at 9:42













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've been stuck on this for 2 days now, and all the resolutions I looked for on StackOverflow and Laracasts proved inconclusive.



I am using Laravel 5.5 with jQuery and I do my testing in Firefox.



My AJAX GET calls are working fine, however, when I try and push an entry in my database, I get a 403 error.



My header does contain the CSRF token:



<meta name="csrf-token" content="{{ csrf_token() }}">


Models are created for every table called in my controller:



public function pushProfile(Request $request){
$userid = Auth::user()->id;
$data = $request->message;
$stat = AppCharacter::where('owner', $userid)->first();
$mess = AppMessage::firstOrCreate([
'posterID' => $userid,
'loc_x' => '0',
'loc_y' => '0',
'characterID' => $stat->id,
'type' => 'profile'
]);
$mess->content = $data;
$mess->save();
return response()->json(['success'=>'Message has been saved!']);
}


Here is the AJAX call, it basically checks for my Quilljs Delta. This Deltais a JSON object formatting a message from WYSIWYG. Then every 5th second, it should try to push it to my database.



I know the Quilljs side works fine because my deltas show properly in my console. But the POST call itself doesn't seem to pass authentication for some reason? (This is just my guess, to me it seems like the only reason it would send a 403.)



setInterval(function() {
if (change.length() > 0) {
console.log('Saving changes', change);
/* AJAX setup */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
headers: {
'Content-Type':'application/json'
},
method: 'POST',
url: '{{ url("/pushProfile") }}',
data: {
message:
{
doc: JSON.stringify(quill.getContents())
},
_token: $('meta[name="csrf-token"]').attr('content')
},
dataType: 'JSON',
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
},
success: function (data) {
$(".writeinfo").append(data.msg);
console.log("Success!!!");
}
});
change = new Delta();
}
}, 5*1000);


To make sure the issue didn't come from CSRF, I went a bit overkill and, after trying to set up the token first in ajaxSetup, then in the AJAX data only, I just assigned it in both. None of these scenarios changed anything.



Of course, I assigned the 'Web' middleware on my post route to check for the above-mentioned CSRF token. The route I use is as follows:



Route::group(['middleware' => ['web']], function () {
Route::post('/pushProfile','MessageSend@pushProfile')->name('pushProfile');
});


I also tried to assign the URL as:



url: '/pushProfile',


To no avail, unfortunately... This just returns a 404 instead of the 403 I currently have:



{
"readyState":4,
"responseText":"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">n<html>
<head>n<title>403 Forbidden</title>n</head>
<body>n<h1>Forbidden</h1>n
<p>You don't have permission to access
/folder/public/{{ route(&quot;pushProfile&quot;) }}n
on this server.<br />n</p>n
<hr>n
<address>Apache/2.4.35 (Win64) PHP/7.2.10 Server at localhost Port 80</address>n
</body>
</html>n",
"status":403,
"statusText":"Forbidden"
}


Did I miss anything?
Thanks!










share|improve this question















I've been stuck on this for 2 days now, and all the resolutions I looked for on StackOverflow and Laracasts proved inconclusive.



I am using Laravel 5.5 with jQuery and I do my testing in Firefox.



My AJAX GET calls are working fine, however, when I try and push an entry in my database, I get a 403 error.



My header does contain the CSRF token:



<meta name="csrf-token" content="{{ csrf_token() }}">


Models are created for every table called in my controller:



public function pushProfile(Request $request){
$userid = Auth::user()->id;
$data = $request->message;
$stat = AppCharacter::where('owner', $userid)->first();
$mess = AppMessage::firstOrCreate([
'posterID' => $userid,
'loc_x' => '0',
'loc_y' => '0',
'characterID' => $stat->id,
'type' => 'profile'
]);
$mess->content = $data;
$mess->save();
return response()->json(['success'=>'Message has been saved!']);
}


Here is the AJAX call, it basically checks for my Quilljs Delta. This Deltais a JSON object formatting a message from WYSIWYG. Then every 5th second, it should try to push it to my database.



I know the Quilljs side works fine because my deltas show properly in my console. But the POST call itself doesn't seem to pass authentication for some reason? (This is just my guess, to me it seems like the only reason it would send a 403.)



setInterval(function() {
if (change.length() > 0) {
console.log('Saving changes', change);
/* AJAX setup */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
headers: {
'Content-Type':'application/json'
},
method: 'POST',
url: '{{ url("/pushProfile") }}',
data: {
message:
{
doc: JSON.stringify(quill.getContents())
},
_token: $('meta[name="csrf-token"]').attr('content')
},
dataType: 'JSON',
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
},
success: function (data) {
$(".writeinfo").append(data.msg);
console.log("Success!!!");
}
});
change = new Delta();
}
}, 5*1000);


To make sure the issue didn't come from CSRF, I went a bit overkill and, after trying to set up the token first in ajaxSetup, then in the AJAX data only, I just assigned it in both. None of these scenarios changed anything.



Of course, I assigned the 'Web' middleware on my post route to check for the above-mentioned CSRF token. The route I use is as follows:



Route::group(['middleware' => ['web']], function () {
Route::post('/pushProfile','MessageSend@pushProfile')->name('pushProfile');
});


I also tried to assign the URL as:



url: '/pushProfile',


To no avail, unfortunately... This just returns a 404 instead of the 403 I currently have:



{
"readyState":4,
"responseText":"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">n<html>
<head>n<title>403 Forbidden</title>n</head>
<body>n<h1>Forbidden</h1>n
<p>You don't have permission to access
/folder/public/{{ route(&quot;pushProfile&quot;) }}n
on this server.<br />n</p>n
<hr>n
<address>Apache/2.4.35 (Win64) PHP/7.2.10 Server at localhost Port 80</address>n
</body>
</html>n",
"status":403,
"statusText":"Forbidden"
}


Did I miss anything?
Thanks!







jquery ajax laravel-5.5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 15:50

























asked Nov 7 at 13:21









mbuss

165




165












  • try url: '{{ route("pushProfile") }}',
    – madalinivascu
    Nov 7 at 13:26












  • Unfortunately, network monitoring still returns a 403 on url: '{{ route("pushProfile") }}', through my localhost. Thanks though :)
    – mbuss
    Nov 7 at 13:33












  • Does allow "post" method your route?
    – FGDeveloper
    Nov 7 at 14:26












  • My route is Route::post, and my controller calls "use IlluminateHttpRequest;". Or do you mean setting up Guards and Providers? I didn't see it as a requirement in any docs :/
    – mbuss
    Nov 8 at 9:42


















  • try url: '{{ route("pushProfile") }}',
    – madalinivascu
    Nov 7 at 13:26












  • Unfortunately, network monitoring still returns a 403 on url: '{{ route("pushProfile") }}', through my localhost. Thanks though :)
    – mbuss
    Nov 7 at 13:33












  • Does allow "post" method your route?
    – FGDeveloper
    Nov 7 at 14:26












  • My route is Route::post, and my controller calls "use IlluminateHttpRequest;". Or do you mean setting up Guards and Providers? I didn't see it as a requirement in any docs :/
    – mbuss
    Nov 8 at 9:42
















try url: '{{ route("pushProfile") }}',
– madalinivascu
Nov 7 at 13:26






try url: '{{ route("pushProfile") }}',
– madalinivascu
Nov 7 at 13:26














Unfortunately, network monitoring still returns a 403 on url: '{{ route("pushProfile") }}', through my localhost. Thanks though :)
– mbuss
Nov 7 at 13:33






Unfortunately, network monitoring still returns a 403 on url: '{{ route("pushProfile") }}', through my localhost. Thanks though :)
– mbuss
Nov 7 at 13:33














Does allow "post" method your route?
– FGDeveloper
Nov 7 at 14:26






Does allow "post" method your route?
– FGDeveloper
Nov 7 at 14:26














My route is Route::post, and my controller calls "use IlluminateHttpRequest;". Or do you mean setting up Guards and Providers? I didn't see it as a requirement in any docs :/
– mbuss
Nov 8 at 9:42




My route is Route::post, and my controller calls "use IlluminateHttpRequest;". Or do you mean setting up Guards and Providers? I didn't see it as a requirement in any docs :/
– mbuss
Nov 8 at 9:42












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I found the solution, and oh god it was so simple I'm ashamed of myself:



I was calling the AJAX from a .js file, using the blade route. Moving the call into a blade file solved the whole issue, as blade routes are only rendered in .blade.php files...



I'm leaving the issue out there in case anyone stumbles upon the same trouble :)






share|improve this answer





















  • Damn, I was just writing that up :-)
    – Don't Panic
    Nov 8 at 16:12










  • Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
    – mbuss
    Nov 8 at 16:46













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190312%2flaravel-5-5-403-forbidden-when-making-ajax-post%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I found the solution, and oh god it was so simple I'm ashamed of myself:



I was calling the AJAX from a .js file, using the blade route. Moving the call into a blade file solved the whole issue, as blade routes are only rendered in .blade.php files...



I'm leaving the issue out there in case anyone stumbles upon the same trouble :)






share|improve this answer





















  • Damn, I was just writing that up :-)
    – Don't Panic
    Nov 8 at 16:12










  • Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
    – mbuss
    Nov 8 at 16:46

















up vote
1
down vote



accepted










I found the solution, and oh god it was so simple I'm ashamed of myself:



I was calling the AJAX from a .js file, using the blade route. Moving the call into a blade file solved the whole issue, as blade routes are only rendered in .blade.php files...



I'm leaving the issue out there in case anyone stumbles upon the same trouble :)






share|improve this answer





















  • Damn, I was just writing that up :-)
    – Don't Panic
    Nov 8 at 16:12










  • Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
    – mbuss
    Nov 8 at 16:46















up vote
1
down vote



accepted







up vote
1
down vote



accepted






I found the solution, and oh god it was so simple I'm ashamed of myself:



I was calling the AJAX from a .js file, using the blade route. Moving the call into a blade file solved the whole issue, as blade routes are only rendered in .blade.php files...



I'm leaving the issue out there in case anyone stumbles upon the same trouble :)






share|improve this answer












I found the solution, and oh god it was so simple I'm ashamed of myself:



I was calling the AJAX from a .js file, using the blade route. Moving the call into a blade file solved the whole issue, as blade routes are only rendered in .blade.php files...



I'm leaving the issue out there in case anyone stumbles upon the same trouble :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 16:11









mbuss

165




165












  • Damn, I was just writing that up :-)
    – Don't Panic
    Nov 8 at 16:12










  • Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
    – mbuss
    Nov 8 at 16:46




















  • Damn, I was just writing that up :-)
    – Don't Panic
    Nov 8 at 16:12










  • Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
    – mbuss
    Nov 8 at 16:46


















Damn, I was just writing that up :-)
– Don't Panic
Nov 8 at 16:12




Damn, I was just writing that up :-)
– Don't Panic
Nov 8 at 16:12












Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
– mbuss
Nov 8 at 16:46






Thanks anyway, @Don't Panic ! (So long, so long, and thanks... For all the fish! ;) )
– mbuss
Nov 8 at 16:46




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190312%2flaravel-5-5-403-forbidden-when-making-ajax-post%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini