ajax调用时出错,通过ajax调用绑定时出错

本文档描述了在ASP.NET MVC应用中使用AJAX调用控制器方法时遇到的问题。问题在于请求成功返回数据,但无法进入success回调,而是触发了error。解决方案包括检查浏览器的网络响应,确认System.Web.Mvc版本一致性,检查Web.config文件中的绑定重定向,以及确保返回的数据结构适合success处理。提供了一个修正后的AJAX和控制器代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("

+%20data%5B0%5D.ImageUrl%20+%20
'" + data[0].Content + "'
");

//$('#AllFacts_Data').append("

+%20data%5B1%5D.ImageUrl%20+%20
'" + data[1].Content + "'
");

},

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值