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');
}
});
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.