jQuery.ajax
attempts to convert the response body depending on the specified dataType
parameter or the Content-Type
header sent by the server.
(jQuery.ajax
尝试根据指定的dataType
参数或服务器发送的Content-Type
标头转换响应主体。)
If the conversion fails (eg if the JSON/XML is invalid), the error callback is fired.(如果转换失败(例如,如果JSON / XML无效),则会触发错误回调。)
Your AJAX code contains:
(您的AJAX代码包含:)
dataType: "json"
In this case jQuery:
(在这种情况下,jQuery:)
Evaluates the response as JSON and returns a JavaScript object.
(将响应评估为JSON并返回一个JavaScript对象。)
[…] The JSON data is parsed in a strict manner;([…] JSON数据以严格的方式进行解析;)
any malformed JSON is rejected and a parse error is thrown.(任何格式错误的JSON都会被拒绝,并引发解析错误。)
[…] an empty response is also rejected;([…]空响应也被拒绝;)
the server should return a response of null or {} instead.(服务器应返回null或{}的响应。)
Your server-side code returns HTML snippet with 200 OK
status.
(您的服务器端代码返回状态为200 OK
HTML代码段。)
jQuery was expecting valid JSON and therefore fires the error callback complaining about parseerror
.(jQuery期望使用有效的JSON,因此引发抱怨parseerror
的错误回调。)
The solution is to remove the dataType
parameter from your jQuery code and make the server-side code return:
(解决方案是从jQuery代码中删除dataType
参数,并使服务器端代码返回:)
Content-Type: application/javascript
alert("Record Deleted");
But I would rather suggest returning a JSON response and display the message inside the success callback:
(但我宁愿建议返回JSON响应并在成功回调中显示消息:)
Content-Type: application/json
{"message": "Record deleted"}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…