删除Ajax适用于localhost - 但不适用于生产托管

时间:2018-01-16 23:13:52

标签: jquery ajax rest

我有一个非常奇怪的错误。

在我当地的xampp环境中,删除操作效果很好,代码完全相同,数据库相同......但是托管它并不起作用。

代码:

<!-- 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>

同样在本地结尾(例如$.ajax({ url: 'http://localhost/dziennik/public/przedmioties/' + id, type: 'DELETE'})

但是你可以看到有一个带有DataTables的Ajax,它在托管和localhost上运行良好。

但删除并不适用于我的托管网站。

为什么呢?我不知道,我无法弄清楚。

关于这段代码,我想:

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

但实际上,它在我的localhost上完全相同(url不同),它正在工作。但是托管它不是。

好的,让我们看一下浏览器的请求......

本地主机:

enter image description here

是的,看起来很好,很正常,而且很有效。尼斯!

但主持人呢?

enter image description here

什么......这太奇怪了。只是,我正在看这个,不知道发生了什么。

这里没有回复:

enter image description here

这个xhr请求失败了:

enter image description here

enter image description here

Ajax请求是一样的,代码是一样的,数据库是一样的,两个路由都结束了......我无法弄明白。

那么这里有什么问题?

5 个答案:

答案 0 :(得分:6)

很可能是您的服务器配置为不允许DELETE请求或某些其他HTTP1.1 +动词。在任何情况下,HTML表单本身都不允许PUTPATCHDELETE操作。虽然这些应该在JavaScript中工作,但值得在您的AJAX请求中尝试form method spoofing。这将绕过您的服务器可能阻止此类请求的任何配置,并且无论您使用的是表单还是AJAX,它都可以正常工作。

如果您仍未收到回复,则需要通过查看Apache访问和错误日​​志来开始调试,并在必要时最终冒泡到框架。

答案 1 :(得分:3)

1 - 主机提供商可能禁用了DELETE。询问托管服务提供商是否启用了DELETE。如果您有权访问apache,请检查是否启用了WebDAV模块,如果没有,请启用它。

2 - 也可能是CORS问题。是否在API上启用了CORS。将您的请求发送到API的CORS标头。 barryvdh/laravel-cors是用于此目的的Laravel API的绝佳包。

3 - 尝试将类型更改为POST并在请求中添加_method =“DELETE”。像这样

$.ajax({ 
         url: 'http://eindeks.000webhostapp.com/przedmioties/' + id, 
         type: 'POST',
         data: {_method: 'delete'},

答案 2 :(得分:2)

您的主机是否允许(EIGEN_CUDACC_VER >= 80000)次请求?如果不是,̶你可以重构控制器并添加操作参数,̶例如,̶和̶DELETE̶请求到您的̶s̶e̶r̶v̶e̶r̶.̶̶̶̶

̶̶功能来存储(请求̶$请求,̶̶$动作̶=̶̶'̶s̶t̶o̶r̶e̶'̶)̶̶{̶̶̶̶̶̶̶̶̶如果̶(̶$动作̶=̶=̶= ̶̶'̶s̶t̶o̶r̶e̶'̶)̶̶{̶̶̶̶̶̶̶̶̶̶̶̶̶/̶/̶存储新的记录̶̶̶̶̶̶̶̶̶}̶ELSE IF̶(̶$动作̶= ̶=̶=̶̶'̶d̶e̶l̶e̶t̶e̶'̶)̶̶{̶̶̶̶̶̶̶̶̶̶̶/̶/̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶ }̶

编辑:方法欺骗

答案 3 :(得分:2)

您需要一步一步解决问题。 第一步是确保您的服务器可以正确接收请求。 每当我需要这样做时,我都会使用这段代码:

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

将代码保存为index.php并向其发出请求。 例如,使用curl:

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

如果您的服务器工作正常,您应该得到类似的响应:

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

如果没有,那么你知道问题所在。

答案 4 :(得分:1)

尝试使用此代码,而不是doDelete

上的当前代码
 function doDelete(id) {
        if (confirm('Naprawdę chcesz usunąć ten wpis?')) {
           $.ajax({ 
             url: 'http://eindeks.000webhostapp.com/przedmioties/' + id, 
             type: 'POST',
             data: {_method: 'delete'},
           }).success(function() {
               theGrid.ajax.reload();
           });
        }
        return false;
 }

这将使laravel路由器转到删除操作,而不需要在DELETE动词的服务器中提供支持。