2
votes

I am making an ajax call from a form to send the checkbox data to a controller. Here is the relevant html code:

 <form method="POST" action="http://192.168.1.25/nonresponsiveurl" accept-charset="UTF-8" class="del_form" id="del_form" name="del_form"><input name="_token" type="hidden" value="igr6iMt1WfeZunyG8wpyy1tNK1efgiclyOvZ1hkF">
  <input id="submit" type="submit" name="submit" value="Delete Checked Records" onclick="return submitDelForm();">
   <div class="table-responsive shadow-edge-nofill">

    <table class="table table-striped">
                    <tbody>
                    <tr>
            <td><input id="badURL0" class="bad_url_checkbox" name="bad_url_checkbox" type="checkbox" value="2"></td>

    etc....etc.....etc....

    </form>

Here is the relevant javascript code:

  <script type="text/javascript">
    function submitDelForm(){
        $('div#loadmoreajaxloader').show();
        var form = document.del_form;
        var dataString = $(form).serialize();
        $.ajaxSetup({
            headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
        });
        $.ajax({
            type: 'POST',
            URL: '/delbadurl',
            contentType: "application/json; charset=utf-8",
            data: {'serial_data' : dataString},
            success: function(data){
                $('div#loadmoreajaxloader').hide();
            }
        });
    }
</script>

Here is relevant routing:

  Route::post('delbadurl','Admin\DashboardController@delBadURL' );

I can confirm that CSRF token is being appended to the dataString. The form data is being appended to the dataString as well. However, jquery throws 405 error.

I am using many other similar ajax functions to fetch and send the data. Everything works perfectly except this particular function. Some pointers will be greatly appreciated as I am coming empty handed.

2
first of all 405 is not a jQuery error, it's a http error. you most likely get this error because the form-url you use (http://192.168.1.25/nonresponsiveurl) does not exist or is not accessible. you are doing nothing to prevent the standard html-submission of the form. so that's what gets you to the 405 page, i guess.low_rents
For the sake of troubleshooting, I suggest you submit the form without AJAX to see what you get...Emeka Mbah
does delBadURL exist in your dashbooardController?Gokigooooks
@low_rents, you are perhaps correct. How do I prevent the standard form submission from occuring? All I want to do is pass the form data to jquery script which will pass it to the controller. The jquery script will rebuild the view after controller executes the code.hvs

2 Answers

1
votes

like I said in my comment, it seems like your form is submitted in the default (HTML) way.

to prevent that you can use jQuery event-handling. in order to do so you should remove the onclick attribute from your <input type="submit" ...> at first.
then bind the event handler to the submit-event and prevent the default behavior of the form submission with preventDefault():

$("#del_form").on("submit", function(event){
    event.preventDefault();
    //your ajax code goes here...
});
0
votes

I think you should do 'url':'/some-text/another-text instead of 'url':'some-text/another-text in your Ajax submission.That is ....there is a / before the first url part.This worked for me.