简体   繁体   中英

Ajax GET request data is not reaching to view from controller in ASP.NET Core MVC on success

In my application in .NET Core (2.2) MVC, I am doing a get request to my controller with user input using ajax.

Here is the code:

 $.ajax({
            type: 'GET',
            url: "/BuildSheetsArea/TesterParameters/GetMassEditing",
            data: { 'ChangeTypeId': ChangeTypeId, 'param': param, 'ParameterValue': ParameterValue },
            dataType: 'json',
            success: function (data) {
                alert('success');
                console.log(data);

            },
            error: function (emp) {
                alert('error');
            }
        });

视图页面中的图像 01

I am getting hit to my controller successfully and my controller is returning the result.

Here is my controller code:

[HttpGet]
public System.Web.Mvc.JsonResult GetMassEditing(string ChangeTypeId, string param, string ParameterValue = "")
{
    List<TesterParameterMassEditingViewModel> finalList = new List<TesterParameterMassEditingViewModel>();

    // Code to perform necessary action

    return new System.Web.Mvc.JsonResult { Data = finalList, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet };
}

Here is a screenshot where controller is returning the result successfully

控制器图片

BUT on success I am not getting anything to my view. As you see in my ajax call on success I am trying to write the data from controller to console, but after controller returning the data nothing is happening. It never goes back to my ajax success function.

Am I missing anything? Kindly help.

I am using .NET Core 2.2 and Entity Framework Core 2.2.6

EDIT: Adding the network tab result screenshot here

网络状态

EDIT2: After @Rena's suggesting I have change my code. but Now I am having a new problem. From my controller when I am trying to do a DB query any in retun tries to pass the object my request goes to pending status. Explanation is below. here is my AXAJ request:

$.ajax({
            type: 'POST',
            url: "/BuildSheetsArea/TesterParameters/GetMassEditing",
            data: {'ChangeTypeId': ChangeTypeId, 'param': param, 'ParameterValue': ParameterValue },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                debugger;
                alert('success');
                console.log(data);
            },
            error: function (emp) {
                alert('error');
            },
        });
    });

And here is my controller code:

        [HttpPost]
    public async Task<JsonResult> GetMassEditing(string ChangeTypeId, string param, string ParameterValue)
    {
        List<TesterParameter> deviceList = await _context.TesterParameters.ToListAsync();
        string val = "See from console";
        return Json(deviceList);
    }

My Device list has 120 objects in it. This case in my network tab in browser I am getting this. My first request is in pending status and I am not getting anything in my ajax success method.

在此处输入图片说明

But if I donot do any db query and simply return a string, it works fine

        [HttpPost]
    public async Task<JsonResult> GetMassEditing(string ChangeTypeId, string param, string ParameterValue)
    {
        string val = "See from console";
        return Json(val);
    }

在此处输入图片说明

In 3rd case if I do the db query only but return the simple string, my network tab shows request in pending and in ajax success method I get nothing.

Please help.

If you are sure that you use asp.net core 2.2,you need to use Microsoft.AspNetCore.Mvc.JsonResult instead of System.Web.Mvc.JsonResult .

Change like below:

[HttpGet]
public Microsoft.AspNetCore.Mvc.JsonResult GetMassEditing(string ChangeTypeId, string param, string ParameterValue = "")
{
    List<TesterParameterMassEditingViewModel> finalList = new List<TesterParameterMassEditingViewModel>();

    // Code to perform necessary action

    return new JsonResult(finalList);
}

Result: 在此处输入图片说明

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