skip to Main Content

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;
    }

2

Answers


  1. 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/

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search