Ajax 删除请求返回 200 但触发错误

问题描述

我正在尝试为我的项目创建一个简单的 API。我正在使用 jquery Ajax 发送 DELETE 请求。删除请求被发送,执行它应该做的事情(从数据库删除一个条目),返回状态 200,但触发一个错误事件。

我已经在这些帖子中寻找了解决方案,但他们无法帮助我:

Ajax request returns 200 but error event is fired Ajax request returns 200 OK but error event is fired Ajax delete returns 200 but firing off error event

这是 AJAX 代码

        function ajaxCall(method,hmm){ // function that send an ajax request
            $.ajax({
                dataType: 'JSON',url: '/APIHandler.PHP?' + $.param({values:hmm}),type: method,success: function(response) { // when the request is done delete the prevIoUsly placed products for new ones
                    console.log(response);
                    const parentDiv = document.querySelector('#basic-grid');
                    removeAllChildNodes(parentDiv);
                    turnToObjects(response);
                },error: function(xhr){
                    alert(xhr);
                    console.log(xhr);
                }
              });
        }
...
ajaxCall('DELETE',checked);

APIHandler 代码

if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
    $vals = $_GET['values'];
    $API->delete($vals);
}

请求响应:

abort: ƒ (e)
arguments: [Exception: TypeError: 'caller','callee',and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.r (<anonymous>:1:83)]
caller: [Exception: TypeError: 'caller',and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.r (<anonymous>:1:83)]
length: 1
name: "abort"
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: jquery.min.js:2
[[Scopes]]: Scopes[3]
always: ƒ ()
catch: ƒ (e)
done: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ (e)
overrideMimeType: ƒ (e)
pipe: ƒ ()
progress: ƒ ()
promise: ƒ (e)
readyState: 4
responseText: ""
setRequestHeader: ƒ (e,t)
state: ƒ ()
status: 200
statusCode: ƒ (e)
statusText: "OK"
then: ƒ (t,n,r)

我也找不到任何错误解决方

[Exception: TypeError: 'caller',and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.r (<anonymous>:1:83)]

解决方法

contentType:'application/json' 添加到您的代码中

并将 dataType 更改为 text

...
dataType: 'text',contentType:'application/json'
...
,

您将 dataType 设置为 JSON,因此 $.ajax 试图将您的 ajax 响应解析为 JSON,但您的响应为空,因此会引发错误。
从您的请求发送 JSON 响应或删除 dataType 参数。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...