I am doing ajax call in Asp.Net MVC with this code
$.ajax({
type: “GET”,
url: ‘@Url.Action(“GetAllFacts”, “Home”)’,
contentType: “application/json; charset=utf-8”,
dataType: “json”,
success: function (data) {
console.log(data);
//$(‘#AllFacts_Data’).append(”
“);
//$(‘#AllFacts_Data’).append(”
“);
},
error: function () {
alert(“Error”);
}
});
This hits to my Get Method GetAllFacts() with following codes
[HttpGet]
public JsonResult GetAllFacts()
{
try
{
using (var context = new DbDemo())
{
var allData_Facts = context.Objblog.Take(2).ToList();
return Json(allData_Facts, JsonRequestBehavior.AllowGet);
}
}
catch (Exception)
{
}
return Json(“false”, JsonRequestBehavior.AllowGet);
}
This is my code which returns list with 2 data properly, but after that it is not going to success method it alerts error as per Ajax error function.
Where I am wrong?
Talk1:
Use your browser tools to inspect the response (Network tab) to see whatthe error is
Talk2:
in console HTML Shows System.IO.FileLoadException: Could not load file or assembly ‘System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Talk3:
I have checked my System.Web.Mvc version in reference and also in Web.config it is 5.0.0.0
Talk4:
Go through your web.config file and check all the version numbers (probably an issue with a binding redirect) but not sure how you even got the page to load if your getting that error.
Talk5:
I matched all the reference version to my Web.Config’s newVersion all are matched. Ans this line return Json(allData_Facts, JsonRequestBehavior.AllowGet); returns 2 data properly as per my requirements.
Solutions1
Try by
Remove assembly reference System.Web.Mvc out of your project.
Use nuget to install System.Web.Mvc for you project.
Verify Web.config to make sure it have System.Web.Mvc assembly.
Run to check.
Good luck!
Solutions2
ajax:
$.ajax({
type: “GET”,
url: ‘/Home/GetAllFacts’,
dataType: “json”,
success: function (data) {
if (data.success) {
// connect to server successful and everything’s ok
// access to server returned data: data.alldata
} else {
// connect to server successful but something went wrong
alert(data.ex); // throw exception message
}
},
error: function () {
// connect to server failure
}
});
controller:
[HttpGet]
public ActionResult GetAllFacts()
{
try
{
using (var context = new DbDemo())
{
var allData_Facts = context.Objblog.Take(2).ToList();
return Json(new { success = true, alldata = allData_Facts }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception e)
{
return Json(new { success = false, ex = e.Message }, JsonRequestBehavior.AllowGet);
}
}