[英]Not getting on Ajax Success from controller JsonResult method
function GetSort() {
var url = '@Url.Action("GetSort", "CatMaster")';
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: null,
success: function (data) {
restrctTrans = false;
$("#Order").data("kendoNumericTextBox").value(data.NxtSort);
},
error: function (data) {
alert('error');
}
});
}
This calls an controller method which returns JSON value: 这将调用一个控制器方法,该方法返回JSON值:
public JsonResult GetSort()
{
var response = _catMaster.GetNextSort();
return Json(response.NxtSort, JsonRequestBehavior.AllowGet);
}
Problem is the ajax success and error function is not called. 问题是ajax成功,并且没有调用错误函数。 But controller returns a value.
但是控制器返回一个值。 Value of
response.NxtSort
is 1 response.NxtSort
值为1
try this 尝试这个
[HttPost]
public JsonResult GetSort()
{
var response = _catMaster.GetNextSort();
return Json(response.NxtSort, JsonRequestBehavior.AllowGet);
}
$("#Order").data("kendoNumericTextBox").value(data);
Replace this with above 用上面替换
$("#Order").data("kendoNumericTextBox").value(data.NxtSort);
"the ajax success and error function is not called. But controller returns a value."
“没有调用ajax成功和错误函数。但是控制器返回一个值。”
This scenario is extremely unlikely to be true. 这种情况极不可能是真的。 If the controller is returning something, then one of those functions will run.
如果控制器正在返回某些东西,那么这些功能之一将运行。 The only way they wouldn't run is if the ajax call fails prior to actually executing, for some other reason.
他们不会运行的唯一方法是,如果出于其他原因,ajax调用在实际执行之前失败了。 You would see the error displayed in your browser's Developer Tools.
您会在浏览器的开发人员工具中看到该错误。
To verify this, you can check your browser's network tab (in the Developer Tools, press F12 on most browsers) and watch for the ajax call to see the HTTP response returned to the browser. 为了验证这一点,您可以检查浏览器的“网络”选项卡(在开发人员工具中,在大多数浏览器上按F12键),并观察ajax调用以查看返回到浏览器的HTTP响应。 If it's "200" then "success" will definitely run.
如果它是“ 200”,那么“成功”肯定会运行。 If it's anything else, "error" will run.
如果还有其他问题,“错误”将运行。 You could put an "alert" command into start of the "success" function to prove that it runs.
您可以在“成功”功能的开始处放置一个“警告”命令,以证明其已运行。
Also check for any errors in your browser's Console as well. 还要在浏览器的控制台中检查是否有任何错误。 I predict that
data.NxtSort
will not exist in the JavaScript context, because as you mentioned, you only return a single integer value from the controller, not an object which could contain a property with that name. 我预测
data.NxtSort
在JavaScript上下文中将不存在,因为正如您提到的,您仅从控制器返回单个整数值,而不是可能包含具有该名称属性的对象的返回值。 data
in the "success" function will simply be an integer with the value 1
. data
中的“成功”功能将简单地与所述值的整数1
。 This means almost certainly there's an "undefined" error happening and showing in the console when you try to access the non-existent nxtSort
property. 这意味着几乎可以肯定,当您尝试访问不存在的
nxtSort
属性时,控制台中会发生“未定义”错误并显示该错误。
This means that, contrary to your statement, "success" is called, but then it crashes without outputting anything. 这意味着,违背了你的说法,“成功” 之称,但随后崩溃而不输出任何东西。
Simply replacing 只需更换
$("#Order").data("kendoNumericTextBox").value(data.NxtSort);
with 与
$("#Order").data("kendoNumericTextBox").value(data);
should solve your problem. 应该可以解决您的问题。
In future if you learn to use your browser's Developer Tools to debug your code, you can avoid simple problems such as this. 将来,如果您学习使用浏览器的开发人员工具调试代码,则可以避免诸如此类的简单问题。 The tools in Chrome and Firefox in particular are extremely powerful and informative.
尤其是Chrome和Firefox中的工具功能强大且信息丰富。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.