Ajax request returns 200 OK, but an error event is fired instead of success(Ajax 请求返回 200 OK,但触发了错误事件而不是成功)
问题描述
我已经在我的网站上实现了一个 Ajax 请求,并且我正在从网页调用端点.它总是返回 200 OK,但 jQuery 会执行错误事件.
我尝试了很多东西,但我无法弄清楚问题所在.我在下面添加我的代码:
I have implemented an Ajax request on my website, and I am calling the endpoint from a webpage. It always returns 200 OK, but jQuery executes the error event.
I tried a lot of things, but I could not figure out the problem. I am adding my code below:
var row = "1";
var json = "{'TwitterId':'" + row + "'}";
$.ajax({
type: 'POST',
url: 'Jqueryoperation.aspx?Operation=DeleteRow',
contentType: 'application/json; charset=utf-8',
data: json,
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
function AjaxSucceeded(result) {
alert("hello");
alert(result.d);
}
function AjaxFailed(result) {
alert("hello1");
alert(result.status + ' ' + result.statusText);
}
JqueryOpeartion.aspx的C#代码
protected void Page_Load(object sender, EventArgs e) {
test();
}
private void test() {
Response.Write("<script language='javascript'>alert('Record Deleted');</script>");
}
成功删除后我需要 ("Record deleted")
字符串.我可以删除内容,但没有收到此消息.这是正确的还是我做错了什么?解决这个问题的正确方法是什么?
I need the ("Record deleted")
string after successful deletion. I am able to delete the content, but I am not getting this message. Is this correct or am I doing anything wrong? What is the correct way to solve this issue?
推荐答案
jQuery.ajax
尝试根据指定的 dataType
参数或服务器发送的 Content-Type
标头转换响应正文.如果转换失败(例如,如果 JSON/XML 无效),则会触发错误回调.
jQuery.ajax
attempts to convert the response body depending on the specified dataType
parameter or the Content-Type
header sent by the server. If the conversion fails (e.g. if the JSON/XML is invalid), the error callback is fired.
您的 AJAX 代码包含:
Your AJAX code contains:
dataType: "json"
在这种情况下是 jQuery:
In this case jQuery:
将响应评估为 JSON 并返回一个 JavaScript 对象.[…]JSON数据被严格解析;任何格式错误的 JSON 都是被拒绝并引发解析错误.[…] 一个空洞的回应也是拒绝;服务器应该返回 null 或 {} 的响应.
Evaluates the response as JSON and returns a JavaScript object. […] The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. […] an empty response is also rejected; the server should return a response of null or {} instead.
您的服务器端代码返回具有 200 OK
状态的 HTML 片段.jQuery 期待有效的 JSON,因此触发错误回调,抱怨 parseerror
.
Your server-side code returns HTML snippet with 200 OK
status. jQuery was expecting valid JSON and therefore fires the error callback complaining about parseerror
.
解决办法是从你的jQuery代码中去掉dataType
参数,让服务端代码返回:
The solution is to remove the dataType
parameter from your jQuery code and make the server-side code return:
Content-Type: application/javascript
alert("Record Deleted");
但我宁愿建议返回 JSON 响应并在成功回调中显示消息:
But I would rather suggest returning a JSON response and display the message inside the success callback:
Content-Type: application/json
{"message": "Record deleted"}
这篇关于Ajax 请求返回 200 OK,但触发了错误事件而不是成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Ajax 请求返回 200 OK,但触发了错误事件而不是成功
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01