简体   繁体   English

无法从控制器JsonResult方法获得Ajax成功

[英]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.

相关问题 jquery ajax调用JsonResult控制器方法在IIS6上调用404 - jquery ajax call to JsonResult controller method results in 404 on IIS6 无法将值从MVC(Razor)视图传递到Controller中的JsonResult方法 - Not able to pass the values from MVC(Razor) view to JsonResult method in Controller 在ajax成功或完成功能中,来自asp.net控制器方法的警报ajax post方法响应 - alert ajax post method response from asp.net controller method in ajax success or in done function 从jquery ajax内JS端的JsonResult获取属性 - Getting properties from JsonResult on the JS side inside jquery ajax 将JSON从控制器反序列化为AJAX成功 - Deserializing JSON from Controller to AJAX Success 返回JsonResult与MVC控制器中的对象列表 - Return JsonResult with List of objects from MVC controller 通过JSONResult从控制器传递数据以进行查看 - Pass data from controller to view via JSONResult Ajax 成功 function 从 Controller 返回后没有调用? MVC - Ajax Success function not calling after returning from Controller ? MVC jQuery Ajax不会从MVC Controller返回成功功能 - jquery ajax is not returning back to success function from MVC Controller 对Controller中的JsonResult进行Ajax调用失败,并显示404错误,“未找到资源” - Ajax call to JsonResult in Controller fails with 404 error, “resource not found”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM