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

Ajax delete request returns 200 but fires error

提问人:Leon Kardaš 提问时间:6/27/2021 更新时间:6/28/2021 访问量:843

问:

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

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

Ajax 请求返回 200,但触发了错误事件 Ajax 请求返回 200 OK,但触发了错误事件 Ajax 删除返回 200,但触发了错误事件

这是 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', '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)]
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', '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)]
javascript php jquery ajax http-delete

评论


答:

3赞 Mohammad 6/27/2021 #1

添加到代码contentType:'application/json'

并更改为dataTypetext

...
dataType: 'text',
contentType:'application/json'
...
1赞 Musa 6/28/2021 #2

你把你的 as 设置为这样 $.ajax 试图将你的 ajax 响应解析为 JSON,但你的响应是空的,所以会抛出错误。
从请求发送 JSON 响应或删除参数。
dataTypeJSONdataType