我已经在我的网站上实现了一个Ajax请求,我正在从一个网页调用端点。它总是返回200个OK,但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);
}

jquery .aspx的c#代码

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

我需要(“记录删除”)成功删除后的字符串。我可以删除内容,但我没有收到这条消息。这是正确的还是我做错了什么?解决这个问题的正确方法是什么?


当前回答

看到这个。这也是一个类似的问题。我努力工作。

不要删除dataType: 'JSON',

注意:您的响应数据应该是json格式

其他回答

我在使用多个空格分隔的数据类型(jQuery 1.5+)方面运气不错。如:

$.ajax({
    type: 'POST',
    url: 'Jqueryoperation.aspx?Operation=DeleteRow',
    contentType: 'application/json; charset=utf-8',
    data: json,
    dataType: 'text json',
    cache: false,
    success: AjaxSucceeded,
    error: AjaxFailed
});

看到这个。这也是一个类似的问题。我努力工作。

不要删除dataType: 'JSON',

注意:您的响应数据应该是json格式

如果你总是从服务器返回JSON(没有空响应),dataType: ' JSON '应该工作,contentType是不需要的。但是,请确保JSON输出…

有效的(JSONLint) 串行化(JSONMinify)

jQuery AJAX将在有效但未序列化的JSON上抛出“parseerror”!

你只需要在AJAX调用中删除dataType: "json"

$.ajax({
    type: 'POST',
    url: 'Jqueryoperation.aspx?Operation=DeleteRow',
    contentType: 'application/json; charset=utf-8',
    data: json,
    dataType: 'json', //**** REMOVE THIS LINE ****//
    cache: false,
    success: AjaxSucceeded,
    error: AjaxFailed
});

我在更新jQuery库时遇到过这个问题。如果服务方法没有返回任何东西,则意味着返回类型为空。

然后在Ajax调用中,请提到dataType='text'。

这将解决问题。