AJAX请求返回200 OK,但错误事件被触发,而不是成功 [英] AJAX request return 200 OK but error event is fired instead of success

查看:1498
本文介绍了AJAX请求返回200 OK,但错误事件被触发,而不是成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了我的网站,我是从网页调用Ajax请求。它总是返回 200确定,但jQuery的执行错误事件。我尝试了很多东西,但无法弄清楚的问题。我加入我下面的code:

I have implemented an AJAX request on my website which I am calling from a webpage. It always returns 200 OK but jQuery executes the error event. I tried a lot of things but could not figure out the problem. I am adding my code below:

jQuery的code

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#$ C $下 JqueryOpeartion.aspx

C# Code for JqueryOpeartion.aspx

protected void Page_Load(object sender, EventArgs e) {
    test();
}
private void test() {
    Response.Write("<script language='javascript'>alert('Record Deleted');</script>");
}

我需要成功删除后的(记录删除)字符串。我能够删除的内容,但我没有得到这个消息。这是正确的还是我做错什么。请建议解决这个问题的正确方式。

I need the ("Record deleted") string after successfully deletion. I am able to delete content but I am not getting this message. Is this correct or am I doing anything wrong. Please suggest the correct way to solve this issue.

推荐答案

您的AJAX请求包含以下设置:

Your AJAX request contains the following setting:

dataType: "json"

借助文档指出jQuery的:

The documentation states that jQuery:

评估响应的JSON,并返回一个JavaScript对象。 (...)   JSON数据解析在严格的方式;任何畸形的JSON是   拒绝,解析错误被抛出。

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.

这意味着,如果服务器返回无效JSON与 200 OK 状态,然后jQuery的触发误差函数,并设置 textStatus 参数parsererror

This means that if server returns invalid JSON with a 200 OK status then jQuery fires the error function and set the textStatus parameter to "parsererror".

解决方法:要确保服务器返回有效 JSON。值得一提的是,一个空的响应也被认为是无效的JSON;你可以返回 {} 的例子验证为JSON。

Solution: make sure that the server returns valid JSON. It is worth noting that an empty response is also considered invalid JSON; you could return {} or null for example which validate as JSON.

您可以检查JSON是否有效不是 jsonlint.com

you can check whether a json is valid or not on jsonlint.com

这篇关于AJAX请求返回200 OK,但错误事件被触发,而不是成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆