Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

javascript - Ajax请求返回200 OK,但是会引发错误事件而不是成功(Ajax request returns 200 OK, but an error event is fired instead of success)

I have implemented an Ajax request on my website, and I am calling the endpoint from a webpage. (我已经在我的网站上实现了Ajax请求,并且正在从网页调用端点。) It always returns 200 OK , but jQuery executes the error event. (它总是返回200 OK ,但是jQuery执行error事件。) I tried a lot of things, but I could not figure out the problem. (我尝试了很多事情,但无法弄清问题所在。) I am adding my code below: (我在下面添加我的代码:)

jQuery Code (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# code for JqueryOpeartion.aspx (JqueryOpeartion.aspx C#代码)

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 successful deletion. (成功删除后,我需要("Record deleted")字符串。) 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? (解决此问题的正确方法是什么?)

  ask by Pankaj Mishra translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...