0
votes

I am working on form updation with ajax. It works fine when i use GET method in ajax but it throws error 405 method not allowed when i use Post method. I am testing this on Localhost. I have done this before in localhost and it worked fine. And by the way i am using Laravel 5.2 for this.

here is my ajax code.

$('#update-modal').on('click',function(){

$.ajax({

    method : "POST",
    url : updateURL,
    data : { client_id : $('#client_id').val(),
             client_name : $('#client_name').val(),
             client_business : $('#client_business').val(),
             client_ref : $('#client_ref').val(),
             gmail_mail : $('#gmail_mail').val(),
             gmail_pass : $('#gmail_pass').val(),
             client_dob : $('#client_dob').val(),
             client_addr : $('#client_addr').val(),
             client_no1 : $('#client_no1').val(),
             client_no2 : $('#client_no2').val(),
             domain_name : $('#domain_name').val(),
             domain_p_date : $('#domain_p_date').val(),
             domain_reg : $('#domain_reg').val(),
             domain_ex_date : $('#domain_ex_date').val(),
             domain_acc_email : $('#domain_acc_email').val(),
             domain_acc_pass :  $('#domain_acc_pass').val()},
             _token : token 

})
.done(function(msg){

    console.log(msg['message']);

});
});

Here is my script used inside the view

        <script>
        var updateURL = '{{ route('updateDomain') }}';
        var token = '{{Session::token()}}';
        </script>

here is my route

Route::post('/updateDomainModal' ,function(\Illuminate\Http\Request $request){      
    return response()->json(['message'=> $request['client_name']]);
})->name('updateDomain');

When the method inside ajax function and Route is changed to GET, It print the client's name passed in the console But when the same is done with POST method it throws the error This is the error details

jquery.min.js:2 GET http://localhost:8000/updateDomainModal?client_id=4&client_name=ABCD&client…2+15%3A01%3A40&domain_acc_email=abc123%40gmail.com&domain_acc_pass=123456 405 (Method Not Allowed)
2
I guess that with post method you need to send data as a payload instead as query parameter. Try JSON.Stringify() on the data before sending - Shubham Khatri
use type:"POST" instead of method:"POST" - Mir Gulam Sarwar
@Mir type is just an alias for method - mdziekon
what content type does the server expect to receive? - Vladimir M
@mdziekon Thanks. May be OP is performing cross domain ajax post - Mir Gulam Sarwar

2 Answers

0
votes

You are wrongly using an } in the line starting with domain_acc_pass. You should use that '}' after assigning the token value. Now, the token won't send to the target, which is required.

0
votes

Use type "POST'

$.ajax({

      type : 'POST',
      url : updateURL,
      data : { client_id : $('#client_id').val(),
               client_name : $('#client_name').val(),
               client_business : $('#client_business').val(),
               client_ref : $('#client_ref').val(),
               gmail_mail : $('#gmail_mail').val(),
               gmail_pass : $('#gmail_pass').val(),
               client_dob : $('#client_dob').val(),
               client_addr : $('#client_addr').val(),
               client_no1 : $('#client_no1').val(),
               client_no2 : $('#client_no2').val(),
               domain_name : $('#domain_name').val(),
               domain_p_date : $('#domain_p_date').val(),
               domain_reg : $('#domain_reg').val(),
               domain_ex_date : $('#domain_ex_date').val(),
               domain_acc_email : $('#domain_acc_email').val(),
               domain_acc_pass :  $('#domain_acc_pass').val()},
               _token : token 

    });

If you submit form

$("#form-name" ).submit(function(ev) {
  ev.preventDefault();  
  var postData = $(this).serializeArray();
  var formURL = $(this).attr("action");
  $.ajax({
    url: formURL,       
    type: 'POST',
    data: postData, 
    success: function(data, textStatus, jqXHR)
    {   
      location.reload();    
    },
    error: function(jqXHR, textStatus, errorThrown)
    {   
      consonle.log("error");
    }
  });
});