![](/img/trans.png)
[英]jQuery Ajax request to Struts2 action class always returns 200 ok, but error event is fired
[英]Ajax request returns 200 OK, but an error event is fired instead of success
我已经在我的网站上实现了一个 Ajax 请求,并且我正在从网页调用端点。 它总是返回200 OK ,但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);
}
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
header 转换响应正文。 如果转换失败(例如,如果 JSON/XML 无效),则会触发错误回调。
您的 AJAX 代码包含:
dataType: "json"
在这种情况下 jQuery:
将响应评估为 JSON 并返回 JavaScript object。 […] JSON 数据被严格解析; 拒绝任何格式错误的 JSON 并引发解析错误。 […] 空的回复也被拒绝; 服务器应返回 null 或 {} 的响应。
您的服务器端代码返回 HTML 片段,状态为200 OK
。 jQuery 期待有效的 JSON 并因此触发错误回调抱怨parseerror
。
解决方案是从您的 jQuery 代码中删除dataType
参数并使服务器端代码返回:
Content-Type: application/javascript
alert("Record Deleted");
但我宁愿建议返回 JSON 响应并在成功回调中显示消息:
Content-Type: application/json
{"message": "Record deleted"}
您只需在 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
});
我在使用多个以空格分隔的dataType
( 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
});
这只是为了记录,因为我在寻找类似于 OP 的问题的解决方案时遇到了这篇文章。
在我的情况下,由于 Chrome 中的同源策略,我的 jQuery Ajax 请求无法成功。 当我修改我的服务器(Node.js)时,一切都解决了:
response.writeHead(200,
{
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "http://localhost:8080"
});
我真的花了一个小时把头撞在墙上。 我感觉自己很傻...
我认为您的 aspx 页面不会返回 JSON object。 你的页面应该做这样的事情(page_load)
var jSon = new JavaScriptSerializer();
var OutPut = jSon.Serialize(<your object>);
Response.Write(OutPut);
另外,尝试更改您的 AjaxFailed:
function AjaxFailed (XMLHttpRequest, textStatus) {
}
textStatus
应该给你你得到的错误类型。
我通过更新的 jQuery 库遇到了这个问题。 如果服务方法没有返回任何内容,则意味着返回类型为void
。
然后在您的 Ajax 通话中请提及dataType='text'
。
它将解决问题。
如果您实施的 Web 服务方法无效,您只需从 header 中删除dataType: 'json'
。
在这种情况下,Ajax 调用预计不会有 JSON 返回数据类型。
使用以下代码确保响应为 JSON 格式(PHP 版本)...
header('Content-Type: application/json');
echo json_encode($return_vars);
exit;
我遇到过同样的问题。 我的问题是我的 controller 返回状态码而不是 JSON。 确保您的 controller 返回如下内容:
public JsonResult ActionName(){
// Your code
return Json(new { });
}
我有类似的问题,但是当我尝试删除 datatype:'json' 时,我仍然遇到了问题。 我的错误正在执行而不是成功
function cmd(){
var data = JSON.stringify(display1());
$.ajax({
type: 'POST',
url: '/cmd',
contentType:'application/json; charset=utf-8',
//dataType:"json",
data: data,
success: function(res){
console.log('Success in running run_id ajax')
//$.ajax({
// type: "GET",
// url: "/runid",
// contentType:"application/json; charset=utf-8",
// dataType:"json",
// data: data,
// success:function display_runid(){}
// });
},
error: function(req, err){ console.log('my message: ' + err); }
});
}
另一件事让我搞砸了,使用localhost
而不是 127.0.0.1 ,反之亦然。 显然,JavaScript 无法处理从一个到另一个的请求。
如果您总是从服务器返回 JSON(没有空响应),则dataType: 'json'
应该可以工作并且不需要contentType
。 但是请确保 JSON output...
jQuery AJAX 将在有效但未序列化的 JSON 上引发“解析错误”!
我有同样的问题。 It was because my JSON response contains some special characters and the server file was not encoded with UTF-8, so the Ajax call considered that this was not a valid JSON response.
您的脚本要求返回 JSON 数据类型。
尝试这个:
private string test() {
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize("hello world");
}
尝试关注
$.ajax({
type: 'POST',
url: 'Jqueryoperation.aspx?Operation=DeleteRow',
contentType: 'application/json; charset=utf-8',
data: { "Operation" : "DeleteRow",
"TwitterId" : 1 },
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
或者
$.ajax({
type: 'POST',
url: 'Jqueryoperation.aspx?Operation=DeleteRow&TwitterId=1',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
cache: false,
success: AjaxSucceeded,
error: AjaxFailed
});
在 JSON object 中使用双引号而不是单引号。 我认为这将解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.