简体   繁体   English

成功 ajax 调用不返回成功 function

[英]successful ajax call not returning to success function

I am working on a .NET Core MVC application, I have added JavaScript code in a partial view, the JavaScript code is hitting function of controller but it is not hitting success function of Ajax call.我正在处理一个 .NET Core MVC 应用程序,我在局部视图中添加了 JavaScript 代码,JavaScript 代码命中 controller 的 function 但它没有命中 881567 调用 883400406589204 的成功Below is my JavaScript code, code is executing on page load下面是我的 JavaScript 代码,代码在页面加载时执行

 <script type="text/javascript"> $(document).ready(function () { debugger; $.ajax({ type: 'GET', url: '@Url.Action("getMenurights","Home")', dataType: 'json', contentType: "application/json; charset=utf-8", success: OnSuccessMenuRights, failure: function (data) { alert(data); } }); }); </script>

controller code:- controller 代码:-

     public string getMenuRights()
     {
        string result = "";
        if (!string.IsNullOrEmpty(HttpContext.Session.GetString("UserId")as string))
        {

            Home objHome = new Home();
            DataTable dtMenuRights = new DataTable();
            dtMenuRights = objHome.GetMenuRights(HttpContext.Session.GetString("UserId").ToString());
            List<DataRow> MenuList = dtMenuRights.AsEnumerable().ToList();

            TempData["MenuRights"] = dtMenuRights;
            if (MenuList.Count > 0)
            {
                ViewBag.MenuList = MenuList;
            }
            TempData.Keep("MenuRights");
            //var x = TempData["MenuRights"];
            JsonSerializerSettings jss = 
            new JsonSerializerSettings { ReferenceLoopHandling =ReferenceLoopHandling.Ignore };
            result = JsonConvert.SerializeObject(dtMenuRights, Formatting.Indented, jss);
        
        }
        return result;
    }

Try these试试这些

  1. You're returning empty string if user has no session. jQuery , as of 1.9, rejects empty responses.如果用户没有jQuery ,您将返回空字符串。从 1.9 开始,jQuery 拒绝空响应。 The response should be null or {} , not empty string.响应应为null{} ,而不是空字符串。 It also rejects malformed JSON but unlikely in this case since you're using a library to generate JSON .它还拒绝格式错误的JSON但在这种情况下不太可能,因为您正在使用库生成JSON

  2. It could be because your return type is string , thus Content-Type header becomes text/plain .可能是因为您的返回类型是string ,因此Content-Type header 变为text/plain Try using either one of these尝试使用其中之一

  • Use dataType: 'text json' in your jQuery call.在您的jQuery电话中使用dataType: 'text json' This will tell jQuery to treat text as JSON .这将告诉 jQuery 将文本视为JSON
  • or return JSON instead of string from the MVC controller using JsonResult as your return type.或使用JsonResult作为返回类型返回JSON而不是来自 MVC controller 的string This should cause Content-Type header to be set to application/json .这应该会导致Content-Type header 被设置为application/json

Here is the spec in details (see dataType )这是详细规范(请参阅dataType

https://api.jquery.com/Jquery.ajax/ https://api.jquery.com/Jquery.ajax/

it is not hitting success function of Ajax call它没有成功拨打 Ajax 的 function

Based on your code, we can find that your action method getMenuRights() return a plain text string to consumer, you can modify the dataType option to 'text' , like below.根据您的代码,我们可以发现您的操作方法getMenuRights()向消费者返回纯文本字符串,您可以将dataType选项修改为'text' ,如下所示。

$.ajax({
    type: 'GET',
    url: '@Url.Action("getMenurights","Home")',
    dataType: 'text',
    success: OnSuccessMenuRights,
    failure: function (data) {
        alert(data);
    }

});

Besides, to troubleshoot Ajax request related issue, you can try to check the response of that ajax request you made in browser F12 developer tool Network tab.此外,要解决 Ajax 请求相关问题,您可以尝试检查您在浏览器 F12 开发者工具网络选项卡中发出的 ajax 请求的响应。 And check if something wrong with the request you made (http 400 error) or server side (http 500 error) etc.并检查您发出的请求(http 400 错误)或服务器端(http 500 错误)等是否有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM