AJAX sending a GET instead of a POST
I'm working on Laravel and trying to send a variable to a controller using AJAX, but the request is changing to GET!
AJAX
function fetchTasks(email) {
$.ajax({
method: 'POST',
dataType: 'json',
url: '/teamwork',
data: {_method: 'POST', email : email},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
}
Routes.php
Route::any('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
When i change the route method to post, it shows a 405(Method Not Allowed)
When i dd($request) in my controller, this is what i get
image
So, why my Ajax request doesn't work ?
EDITED:
I've modified my code to the following
function fetchTasks(email) {
console.log(email);
var token = "{{ csrf_token() }}";
$.ajax({
method: "POST",
url: "teamwork",
data: {
_token:token,
'email': email
},
contentType: "application/json",
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
},
complete: function () {
window.location.href = '{{route("testTRoute")}}';
}
});
}
It's still sending an empty GET request. Output from console is the following:
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
php ajax laravel
add a comment |
I'm working on Laravel and trying to send a variable to a controller using AJAX, but the request is changing to GET!
AJAX
function fetchTasks(email) {
$.ajax({
method: 'POST',
dataType: 'json',
url: '/teamwork',
data: {_method: 'POST', email : email},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
}
Routes.php
Route::any('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
When i change the route method to post, it shows a 405(Method Not Allowed)
When i dd($request) in my controller, this is what i get
image
So, why my Ajax request doesn't work ?
EDITED:
I've modified my code to the following
function fetchTasks(email) {
console.log(email);
var token = "{{ csrf_token() }}";
$.ajax({
method: "POST",
url: "teamwork",
data: {
_token:token,
'email': email
},
contentType: "application/json",
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
},
complete: function () {
window.location.href = '{{route("testTRoute")}}';
}
});
}
It's still sending an empty GET request. Output from console is the following:
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
php ajax laravel
How do you call ajax? Any Button press or menu link (a tag)? I doubt you use ajax with a link. If so, it will not work.
– Md. Harun Or Rashid
Nov 23 '18 at 1:16
I validate if the user email exists in the database first. If so, i call the function with "fetchtasks( );". I think it's working because as you can see in the image it changes the route to "/teamwork"
– Maram AlSaegh
Nov 23 '18 at 1:46
add a comment |
I'm working on Laravel and trying to send a variable to a controller using AJAX, but the request is changing to GET!
AJAX
function fetchTasks(email) {
$.ajax({
method: 'POST',
dataType: 'json',
url: '/teamwork',
data: {_method: 'POST', email : email},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
}
Routes.php
Route::any('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
When i change the route method to post, it shows a 405(Method Not Allowed)
When i dd($request) in my controller, this is what i get
image
So, why my Ajax request doesn't work ?
EDITED:
I've modified my code to the following
function fetchTasks(email) {
console.log(email);
var token = "{{ csrf_token() }}";
$.ajax({
method: "POST",
url: "teamwork",
data: {
_token:token,
'email': email
},
contentType: "application/json",
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
},
complete: function () {
window.location.href = '{{route("testTRoute")}}';
}
});
}
It's still sending an empty GET request. Output from console is the following:
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
php ajax laravel
I'm working on Laravel and trying to send a variable to a controller using AJAX, but the request is changing to GET!
AJAX
function fetchTasks(email) {
$.ajax({
method: 'POST',
dataType: 'json',
url: '/teamwork',
data: {_method: 'POST', email : email},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
}
Routes.php
Route::any('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
When i change the route method to post, it shows a 405(Method Not Allowed)
When i dd($request) in my controller, this is what i get
image
So, why my Ajax request doesn't work ?
EDITED:
I've modified my code to the following
function fetchTasks(email) {
console.log(email);
var token = "{{ csrf_token() }}";
$.ajax({
method: "POST",
url: "teamwork",
data: {
_token:token,
'email': email
},
contentType: "application/json",
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
},
complete: function () {
window.location.href = '{{route("testTRoute")}}';
}
});
}
It's still sending an empty GET request. Output from console is the following:
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
php ajax laravel
php ajax laravel
edited Nov 23 '18 at 11:18
Maram AlSaegh
asked Nov 23 '18 at 0:53
Maram AlSaeghMaram AlSaegh
4512
4512
How do you call ajax? Any Button press or menu link (a tag)? I doubt you use ajax with a link. If so, it will not work.
– Md. Harun Or Rashid
Nov 23 '18 at 1:16
I validate if the user email exists in the database first. If so, i call the function with "fetchtasks( );". I think it's working because as you can see in the image it changes the route to "/teamwork"
– Maram AlSaegh
Nov 23 '18 at 1:46
add a comment |
How do you call ajax? Any Button press or menu link (a tag)? I doubt you use ajax with a link. If so, it will not work.
– Md. Harun Or Rashid
Nov 23 '18 at 1:16
I validate if the user email exists in the database first. If so, i call the function with "fetchtasks( );". I think it's working because as you can see in the image it changes the route to "/teamwork"
– Maram AlSaegh
Nov 23 '18 at 1:46
How do you call ajax? Any Button press or menu link (a tag)? I doubt you use ajax with a link. If so, it will not work.
– Md. Harun Or Rashid
Nov 23 '18 at 1:16
How do you call ajax? Any Button press or menu link (a tag)? I doubt you use ajax with a link. If so, it will not work.
– Md. Harun Or Rashid
Nov 23 '18 at 1:16
I validate if the user email exists in the database first. If so, i call the function with "fetchtasks( );". I think it's working because as you can see in the image it changes the route to "/teamwork"
– Maram AlSaegh
Nov 23 '18 at 1:46
I validate if the user email exists in the database first. If so, i call the function with "fetchtasks( );". I think it's working because as you can see in the image it changes the route to "/teamwork"
– Maram AlSaegh
Nov 23 '18 at 1:46
add a comment |
4 Answers
4
active
oldest
votes
Based on ajax documentation you should use type param instead method.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
add a comment |
Have u tried this?
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url:'teamwork' ,
type:'post',
data: { email : email},
method: 'POST',
dataType: 'json',
success:function(result){console.log(result);}
});
Route
Route::match(array('GET','POST'),'/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
add a comment |
If you want to send 'email' as a route parameter but don't want to show it in browser's address bar, you can do it as below.
Sending data through a form
At you blade.php
<form action="{{route('testPRoute')}}" method="POST">
@csrf
<!--
Set your email name or variable in input's value attribute. Like
<input type="text" name="email" value="email">
or
<input type="text" name="email" value="{{$email}}">
or -->
<input type="hidden" name="email" value="email">
<button type="submit">Go to Route</button>
</form>
At your Web.php
Route::post('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
At your Controller
public function teamwork(Request $request)
{
$email = $request->email;
return $email;
}
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
add a comment |
After a long time of debugging, i have found that my problem was with the routing. I had two routes, GET and POST with the same name. That's why it was always sending a GET request.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439504%2fajax-sending-a-get-instead-of-a-post%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on ajax documentation you should use type param instead method.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
add a comment |
Based on ajax documentation you should use type param instead method.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
add a comment |
Based on ajax documentation you should use type param instead method.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
Based on ajax documentation you should use type param instead method.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
answered Nov 23 '18 at 1:12
Sergio Alexander FlorezSergio Alexander Florez
1
1
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
add a comment |
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
I've tried both. Still not working
– Maram AlSaegh
Nov 23 '18 at 1:43
add a comment |
Have u tried this?
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url:'teamwork' ,
type:'post',
data: { email : email},
method: 'POST',
dataType: 'json',
success:function(result){console.log(result);}
});
Route
Route::match(array('GET','POST'),'/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
add a comment |
Have u tried this?
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url:'teamwork' ,
type:'post',
data: { email : email},
method: 'POST',
dataType: 'json',
success:function(result){console.log(result);}
});
Route
Route::match(array('GET','POST'),'/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
add a comment |
Have u tried this?
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url:'teamwork' ,
type:'post',
data: { email : email},
method: 'POST',
dataType: 'json',
success:function(result){console.log(result);}
});
Route
Route::match(array('GET','POST'),'/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
Have u tried this?
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url:'teamwork' ,
type:'post',
data: { email : email},
method: 'POST',
dataType: 'json',
success:function(result){console.log(result);}
});
Route
Route::match(array('GET','POST'),'/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
answered Nov 23 '18 at 4:55
DPSDPS
491212
491212
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
add a comment |
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
Same issue nothing changed
– Maram AlSaegh
Nov 23 '18 at 9:16
add a comment |
If you want to send 'email' as a route parameter but don't want to show it in browser's address bar, you can do it as below.
Sending data through a form
At you blade.php
<form action="{{route('testPRoute')}}" method="POST">
@csrf
<!--
Set your email name or variable in input's value attribute. Like
<input type="text" name="email" value="email">
or
<input type="text" name="email" value="{{$email}}">
or -->
<input type="hidden" name="email" value="email">
<button type="submit">Go to Route</button>
</form>
At your Web.php
Route::post('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
At your Controller
public function teamwork(Request $request)
{
$email = $request->email;
return $email;
}
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
add a comment |
If you want to send 'email' as a route parameter but don't want to show it in browser's address bar, you can do it as below.
Sending data through a form
At you blade.php
<form action="{{route('testPRoute')}}" method="POST">
@csrf
<!--
Set your email name or variable in input's value attribute. Like
<input type="text" name="email" value="email">
or
<input type="text" name="email" value="{{$email}}">
or -->
<input type="hidden" name="email" value="email">
<button type="submit">Go to Route</button>
</form>
At your Web.php
Route::post('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
At your Controller
public function teamwork(Request $request)
{
$email = $request->email;
return $email;
}
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
add a comment |
If you want to send 'email' as a route parameter but don't want to show it in browser's address bar, you can do it as below.
Sending data through a form
At you blade.php
<form action="{{route('testPRoute')}}" method="POST">
@csrf
<!--
Set your email name or variable in input's value attribute. Like
<input type="text" name="email" value="email">
or
<input type="text" name="email" value="{{$email}}">
or -->
<input type="hidden" name="email" value="email">
<button type="submit">Go to Route</button>
</form>
At your Web.php
Route::post('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
At your Controller
public function teamwork(Request $request)
{
$email = $request->email;
return $email;
}
If you want to send 'email' as a route parameter but don't want to show it in browser's address bar, you can do it as below.
Sending data through a form
At you blade.php
<form action="{{route('testPRoute')}}" method="POST">
@csrf
<!--
Set your email name or variable in input's value attribute. Like
<input type="text" name="email" value="email">
or
<input type="text" name="email" value="{{$email}}">
or -->
<input type="hidden" name="email" value="email">
<button type="submit">Go to Route</button>
</form>
At your Web.php
Route::post('/teamwork', 'TeamworkController@teamwork')->name('testPRoute');
At your Controller
public function teamwork(Request $request)
{
$email = $request->email;
return $email;
}
edited Nov 23 '18 at 5:25
answered Nov 23 '18 at 5:17
Md. Harun Or RashidMd. Harun Or Rashid
4611722
4611722
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
add a comment |
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
Not sure if i can or how i can use a form to send it. I don't have a form for user login. Instead i'm using google login like this example developers.google.com/identity/sign-in/web
– Maram AlSaegh
Nov 23 '18 at 9:27
add a comment |
After a long time of debugging, i have found that my problem was with the routing. I had two routes, GET and POST with the same name. That's why it was always sending a GET request.
add a comment |
After a long time of debugging, i have found that my problem was with the routing. I had two routes, GET and POST with the same name. That's why it was always sending a GET request.
add a comment |
After a long time of debugging, i have found that my problem was with the routing. I had two routes, GET and POST with the same name. That's why it was always sending a GET request.
After a long time of debugging, i have found that my problem was with the routing. I had two routes, GET and POST with the same name. That's why it was always sending a GET request.
answered Dec 4 '18 at 17:26
Maram AlSaeghMaram AlSaegh
4512
4512
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439504%2fajax-sending-a-get-instead-of-a-post%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
How do you call ajax? Any Button press or menu link (a tag)? I doubt you use ajax with a link. If so, it will not work.
– Md. Harun Or Rashid
Nov 23 '18 at 1:16
I validate if the user email exists in the database first. If so, i call the function with "fetchtasks( );". I think it's working because as you can see in the image it changes the route to "/teamwork"
– Maram AlSaegh
Nov 23 '18 at 1:46