加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

前端 ajax 获取后台json数据 解析

发布时间:2020-12-16 03:02:46 所属栏目:百科 来源:网络整理
导读:先贴代码 function edit(node) { var customerid = $(node).parents( " tr " ).children().eq( 0 ).text(); alert(customerid) $.ajax({ type: " post " ,url: " /IsCreateCoustomer/AddButtes?id= " + customerid,// dataType: " 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"] 来访问。实在没必要。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读