我已在我的网站上实现了 Ajax 请求,并且正在从网页调用端点。它总是会回来200 OK, but jQuery执行错误事件。
我尝试了很多东西,但我无法找出问题所在。我在下面添加我的代码:
jQuery 代码
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);
}
C# 代码JqueryOpeartion.aspx
protected void Page_Load(object sender, EventArgs e) {
test();
}
private void test() {
Response.Write("<script language='javascript'>alert('Record Deleted');</script>");
}
我需要("Record deleted")
删除成功后的字符串。我可以删除内容,但没有收到此消息。这是正确的还是我做错了什么?解决这个问题的正确方法是什么?
jQuery.ajax尝试根据指定的内容转换响应正文dataType
参数或Content-Type
服务器发送的标头。如果转换失败(例如,如果 JSON/XML 无效),则会触发错误回调。
您的 AJAX 代码包含:
dataType: "json"
在本例中,jQuery:
将响应评估为 JSON 并返回 JavaScript 对象。 […]
JSON数据经过严格解析;任何格式错误的 JSON 都是
被拒绝并抛出解析错误。 [...] 空响应也是
拒绝;服务器应该返回 null 或 {} 响应。
您的服务器端代码返回 HTML 片段200 OK
地位。 jQuery 期望有效的 JSON,因此触发错误回调抱怨parseerror
.
解决方案是删除dataType
来自 jQuery 代码的参数并使服务器端代码返回:
Content-Type: application/javascript
alert("Record Deleted");
但我宁愿建议返回 JSON 响应并在成功回调中显示消息:
Content-Type: application/json
{"message": "Record deleted"}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)