简体   繁体   中英

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.Below is my JavaScript code, code is executing on page load

 <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:-

     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. The response should be null or {} , not empty string. It also rejects malformed JSON but unlikely in this case since you're using a library to generate JSON .

  2. It could be because your return type is string , thus Content-Type header becomes text/plain . Try using either one of these

  • Use dataType: 'text json' in your jQuery call. This will tell jQuery to treat text as JSON .
  • or return JSON instead of string from the MVC controller using JsonResult as your return type. This should cause Content-Type header to be set to application/json .

Here is the spec in details (see dataType )

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

it is not hitting success function of Ajax call

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.

$.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. And check if something wrong with the request you made (http 400 error) or server side (http 500 error) etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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