前端 ajax 获取后台json数据 解析
先贴代码
function edit(node) {
var customerid = $(node).parents("tr").children().eq(0).text();
alert(customerid)
$.ajax({
type: "post",
url: "/IsCreateCoustomer/AddButtes?id=" + customerid,
// dataType: "json", //这个数据传输格式一开始我是没写的,因为没意识到她的用处,以为传数据有用,接数据没用,真是无知
success: function (res) {
if (res != "nobutt") {
var butt = eval(res);//这个可以将字符串转化为变量对象
console.log("buttid 0" + butt[0]["buttid"] + "buttname " + butt[0]["buttname"] + "butt_tel " + butt[0]["butt_tel"] + "accountname: " + butt[0]["accountname"]);
//for(k=0;k < butt.length;k++){
// console.log("buttid kk " + butt[k]["buttid"] +" ggg"+ butt[k].buttid+ "buttname " + butt[k].buttname + "butt_tel " + butt[k]["butt_tel"] + "accountname: " + butt[k]["accountname"]);
//}
$.each(res ,function (i,butt) {
// console.log(" each buttid " + butt["buttid"] + "buttname " + butt["buttname"] + "butt_tel " + butt["butt_tel"] + "accountname: " + butt["accountname"])
console.log("索引 :" + i)
console.log("butt :" + butt.buttname)
})
//$.each(function (i, butt) {
// console.log(butt[i])
// console.log("buttid " + butt[i]["buttid"] + "buttname " + butt[i]["buttname"] + "butt_tel " + butt[i]["butt_tel"] + "accountname: " + butt[i]["accountname"])
//})
} else {
alert("!!!")
}
}
})
$("#model").modal('show');
$("#edit").attr("src", "/IsCreateCoustomer/AddCoustomer?oper=update&&id=" + customerid);
$(".modal-title").append("编辑");
$("#edit").load();
}
//后端代码
[HttpPost]
public ActionResult AddButtes(int id) {
bool s = (new ButtDAO().GetCoustButtList().SingleOrDefault().coustomerid == id);
if (s)
{
List<CoustomerButtInfo> CoustomerButtList = new ButtDAO().GetCoustomerButtList(e => e.coustomerid == id).ToList();
List<buttinfo> buttList = new List<buttinfo>();
for (int i = 0; i < CoustomerButtList.Count; i++)
{
buttList.Add(new ButtDAO().GetModelList(e => e.buttid == CoustomerButtList[i].buttid).FirstOrDefault());
}
string result = JsonConvert.SerializeObject(buttList);
return Content(result);
}
else {
//无对接人
return Content("nobutt");
}
}
第一点:正是上面所说,因为没有写接受数据类型(dataType:json),导致我接收的返回数据是字符串类型。
然后:(导致我怀疑人生—)?? 凭啥我的each 遍历不了数据(实在搞不了,只有将接收到的res 通过eval()封装成变量)
第二点:很让我惊喜的是,后台返回的数据(它是一个对象)居然可以通过 “.” 来访问,很骚啊!哥,我传过来的是一个数组,
里面封装的是对象,还以为只能通过butt["buttid"] 来访问。实在没必要。
大道三千,条条皆可证道。道心不可失。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现