Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Ajax works in localhost - but doesn't in production hosting

Tags:

rest

jquery

ajax

I have a really strange bug.

In my local xampp's environment, the delete operation works great, the code is exactly the same, the database the same... And yet on hosting it doesn't work.

Code:

<!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.0.0/js/dataTables.responsive.min.js"></script>

    <script>
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
    </script>    

        <script type="text/javascript">
        var theGrid = null;
        $(document).ready(function(){
            theGrid = $('#thegrid').DataTable({
                "processing": true,
                "serverSide": true,
                "ordering": true,
                "responsive": true,
                "ajax": "http://eindeks.000webhostapp.com/przedmioties/grid",
                "columnDefs": [
                    {
                        "render": function ( data, type, row ) {
                            return '<a href="http://eindeks.000webhostapp.com/przedmioties/'+row[0]+'">'+data+'</a>';
                        },
                        "targets": 1
                    },
                    {
                        "render": function ( data, type, row ) {
                            return '<a href="http://eindeks.000webhostapp.com/przedmioties/'+row[0]+'/edit" class="btn btn-default">Zaktualizuj</a>';
                        },
                        "targets": 4                    },
                    {
                        "render": function ( data, type, row ) {
                            return '<a href="#" onclick="return doDelete('+row[0]+')" class="btn btn-danger">Usuń</a>';
                        },
                        "targets": 4+1
                    },
                ]
            });
        });
        function doDelete(id) {
            if(confirm('Naprawdę chcesz usunąć ten wpis?')) {
               $.ajax({ url: 'http://eindeks.000webhostapp.com/przedmioties/' + id, type: 'DELETE'}).success(function() {
                theGrid.ajax.reload();
               });
            }
            return false;
        }
    </script>

The same is on local's end (e.g. $.ajax({ url: 'http://localhost/dziennik/public/przedmioties/' + id, type: 'DELETE'}))

But as you can see there is an Ajax with DataTables and it works well on both hosting and localhost.

But deletion doesn't work on my hosting's website.

Why? I don't know and I can't figure it out.

It's about this code I think:

function doDelete(id) {
            if(confirm('Naprawdę chcesz usunąć ten wpis?')) {
               $.ajax({ url: 'http://eindeks.000webhostapp.com/przedmioties/' + id, type: 'DELETE'}).success(function() {
                theGrid.ajax.reload();
               });
            }
            return false;
        }

But really, it's exactly the same (url different) on my localhost wherein which it is working. But on hosting it is not.

Okay, let's see the requests from the browser...

Localhost:

enter image description here

Yea, looks good, normal, and works. Nice!

But what on hosting?

enter image description here

What the... This is so weird. Just, I am looking at this and have no idea what is going on.

There is no response here:

enter image description here

And this xhr request has failed:

enter image description here

enter image description here

Ajax request is the same, code is the same, database is the same, both route ends working... I can't figure it out.

So what is the problem here?

like image 781
Krystian Polska Avatar asked Jan 16 '18 23:01

Krystian Polska


3 Answers

It could very well be that your server is configured not to allow DELETE requests or some of the other HTTP1.1+ verbs. In any case, HTML forms themselves don't allow PUT, PATCH, or DELETE actions. While these should work in JavaScript, it is worth giving form method spoofing a try in your AJAX request. This will circumvent any configuration your server might have that would block such a request and also work whether you're using a form or AJAX.

If you're still not getting a response, you'll need to start debugging by looking through Apache access and error logs and eventually bubbling up to the framework if necessary.

like image 99
samrap Avatar answered Sep 30 '22 15:09

samrap


1 - It might be that DELETE is disabled by the hosting provider. Ask the hosting provider whether DELETE is enabled. If you have access to apache check whether WebDAV module is enabled and if not, enable it.

2 - It might be CORS issue as well. Is CORS enabled on API. Send CORS headers with your request to API. barryvdh/laravel-cors is a great package for Laravel APIs for this purpose.

3 - Try changing type to POST and adding _method="DELETE" to the request. like this

$.ajax({ 
         url: 'http://eindeks.000webhostapp.com/przedmioties/' + id, 
         type: 'POST',
         data: {_method: 'delete'},
like image 32
aimme Avatar answered Sep 30 '22 16:09

aimme


You need to approach the problem step by step. The first step is making sure that your server can receive the request properly. Whenever I need to do that, I use this code:

<?php
header('Content-Type: application/json');
echo json_encode([
    'method' => $_SERVER['REQUEST_METHOD'],
    'uri' => $_SERVER['REQUEST_URI'],
    'body' => file_get_contents('php://input'),
    'headers' => getallheaders(),
], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

Save the code as index.php and make a request to it. For instance, with curl:

curl -XDELETE http://example.com/index.php

If your server is working properly, you should get a response similar to this:

{
    "method": "DELETE",
    "uri": "/index.php",
    "body": "",
    "headers": {
        "Host": "example.com",
        "User-Agent": "curl/7.53.1",
        "Accept": "*/*"
    }
}

If not, then you know where the problem is.

like image 26
Rei Avatar answered Sep 30 '22 14:09

Rei