[英]AJAX Submit - Status 200 with error
I realise there are already questions about this, but none have been able to solve my problem. 我意识到已经有关于此的问题,但没有一个能够解决我的问题。
I'm trying to submit a form in a Jquery Modal using AJAX, but it's not working despite saying status 200. Here's the code: 我正在尝试使用AJAX在Jquery Modal中提交表单,但尽管状态为200,但仍无法正常工作。这是代码:
$(document).ready(function () {
$("#dialog-form").dialog({
autoOpen: false,
});
$("#add-course").click(function () {
$("#dialog-form").dialog("open");
return false;
});
$("#btncancel").click(function () {
$("#dialog-form").dialog("close");
});
$('#<%=btnsubmit.ClientID%>').on('click', function (e) {
e.preventDefault();
var courseDetails = {};
courseDetails.Code = $('#<%=addcode.ClientID%>').val();
courseDetails.Name = $('#<%=addname.ClientID%>').val();
var jsonData = JSON.stringify({
courseDetails: courseDetails
});
$.ajax({
url: '<%=ResolveUrl("updatetest.aspx/addCourseSP") %>',
type: "POST",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnErrorCall,
});
function OnSuccess(response, jqXHR, textStatus, errorThrown) {
var result = response.d;
if (result == "success") {
$("#msg").html("Success");
$("#dialog-form").dialog("close");
} else {
$("#msg").html("jqXHR: " + JSON.stringify(jqXHR) + " " + "AJAX error: " + textStatus + ' : ' + errorThrown);
}
}
function OnErrorCall() {
$("#msg").html("error");
}
});
});
The code behind calls a stored procedure which I checked works. 后面的代码调用了我检查过的存储过程。 I've also checked the JSON string at jsonlint.com and it says it's valid.
我还在jsonlint.com上检查了JSON字符串,并说它是有效的。
I've checked using F12 in browser (Chrome/Firefox) which basically says the same thing, status 200 with d:"error". 我已经在浏览器(Chrome / Firefox)中使用F12进行了检查,该浏览器基本上说的是同一件事,状态为200,带有d:“错误”。 The JSON string is under Params, so I don't think that's the issue.
JSON字符串位于Params下,因此我认为这不是问题。
I've tried debugging using various different ways, right now it's returning jqXHR: "success" AJAX error: [object Object] : undefined. 我尝试使用各种不同的方式进行调试,现在它返回jqXHR:“成功” AJAX错误:[object Object]:未定义。 I'm not sure what else to try.
我不确定还有什么尝试。 Anyone have any ideas?
有人有想法么?
jQuery.ajax() shows the success callback as: jQuery.ajax()将成功回调显示为:
success
成功
Type: Function( Anything data, String textStatus, jqXHR jqXHR )
类型:函数(任何数据,字符串textStatus,jqXHR jqXHR)
Doesn't match your parameter list 与您的参数列表不匹配
function OnSuccess(response, jqXHR, textStatus, errorThrown)
When you try to use this in your script, textStatus
is not a string and errorThrown
is undefined . 当您尝试在脚本中使用它时,
textStatus
不是字符串,并且errorThrown
是undefined 。
So the request succeeded (Status 200) but your script is generating the error after the response is returned to the client. 因此,请求成功(状态200),但是在响应返回到客户端之后,您的脚本正在生成错误。
your function contains the following line 您的函数包含以下行
dataType: "json",
it means data returned must be correctly formatted json, if it not it will consider it error 这意味着返回的数据必须正确格式化json,如果不正确,它将认为它是错误的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.