AJAX sending a GET instead of a POST












-1















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: ƒ, …}









share|improve this question

























  • 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


















-1















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: ƒ, …}









share|improve this question

























  • 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
















-1












-1








-1








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: ƒ, …}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





















  • 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














4 Answers
4






active

oldest

votes


















0














Based on ajax documentation you should use type param instead method.



$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});





share|improve this answer
























  • I've tried both. Still not working

    – Maram AlSaegh
    Nov 23 '18 at 1:43



















0














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');





share|improve this answer
























  • Same issue nothing changed

    – Maram AlSaegh
    Nov 23 '18 at 9:16



















0














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;
}





share|improve this answer


























  • 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





















0














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    Based on ajax documentation you should use type param instead method.



    $.ajax({
    type: "POST",
    url: url,
    data: data,
    success: success,
    dataType: dataType
    });





    share|improve this answer
























    • I've tried both. Still not working

      – Maram AlSaegh
      Nov 23 '18 at 1:43
















    0














    Based on ajax documentation you should use type param instead method.



    $.ajax({
    type: "POST",
    url: url,
    data: data,
    success: success,
    dataType: dataType
    });





    share|improve this answer
























    • I've tried both. Still not working

      – Maram AlSaegh
      Nov 23 '18 at 1:43














    0












    0








    0







    Based on ajax documentation you should use type param instead method.



    $.ajax({
    type: "POST",
    url: url,
    data: data,
    success: success,
    dataType: dataType
    });





    share|improve this answer













    Based on ajax documentation you should use type param instead method.



    $.ajax({
    type: "POST",
    url: url,
    data: data,
    success: success,
    dataType: dataType
    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    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');





    share|improve this answer
























    • Same issue nothing changed

      – Maram AlSaegh
      Nov 23 '18 at 9:16
















    0














    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');





    share|improve this answer
























    • Same issue nothing changed

      – Maram AlSaegh
      Nov 23 '18 at 9:16














    0












    0








    0







    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');





    share|improve this answer













    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');






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 4:55









    DPSDPS

    491212




    491212













    • 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





    Same issue nothing changed

    – Maram AlSaegh
    Nov 23 '18 at 9:16











    0














    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;
    }





    share|improve this answer


























    • 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


















    0














    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;
    }





    share|improve this answer


























    • 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
















    0












    0








    0







    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;
    }





    share|improve this answer















    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;
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • 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













    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 17:26









        Maram AlSaeghMaram AlSaegh

        4512




        4512






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python