当前位置:   article > 正文

【C#】ajax接收JsonResult返回字符串后如何使用_c# jsonresult

c# jsonresult

应用场景:

通过ajax向控制器传值并接收返回值于data中,且返回数据需在前端输入框中显示


后端:

  1. [AllowAnonymous]
  2. public JsonResult getCardReader()
  3. {
  4. //StreamReader reader = new StreamReader(Path, System.Text.Encoding.GetEncoding("UTF-8"));
  5. //启动卡号读卡器ReadCardState 0禁用1检测2完成
  6. List<CRKCommunication> CRKCom = CRKCommunicationservice.GetModels(p => true).ToList();
  7. //if (CRKCom.Count > 0)
  8. //{
  9. // CRKCom[0].ReadCardState = 1;
  10. // CRKCommunicationservice.Update(CRKCom[0]);
  11. //}
  12. CRKCom[0].ReadCardState = 1;
  13. CRKCommunicationservice.Update(CRKCom[0]);
  14. //文件路径 取字符串
  15. string Path = @"C:\Users\Administrator\Desktop\kh.txt";
  16. string kno = System.IO.File.ReadAllText(Path, System.Text.Encoding.Default);
  17. string SmartCardNumber = "";
  18. string Number = "41018105" + kno + DateTime.Now.ToString("yyyyMMdd");
  19. //智慧卡号列表(数据库中与当前时间卡号相同,且卡号状态不为为使用中 的数据)
  20. List<card_info> cardList = card_infoservice.GetModels(p => true).Where<card_info>(p => p.Card_State != "1" && p.BusinessNumber.Substring(0, Number.Length) == Number).ToList();
  21. SmartCardNumber = Number + (cardList.Count + 1).ToString("000");
  22. var res = new JsonResult();
  23. res.Data = "{\"kno\"" + ":" + kno + ",\"SmartCardNumber\"" + ":" + "\"" + SmartCardNumber + "\"" + "}";
  24. return res;
  25. }

前端 ajax:

  1. <html>
  2. <head>
  3. <script>
  4. form.on("submit(CardReader)", function (data) {
  5. //layer.msg(JSON.stringify(data.field));
  6. laycy.ajax({
  7. url: '/StockOI/getCardReader',
  8. type: 'post',
  9. data: data.field,
  10. dataType: "json",
  11. success: function (da) {
  12. var obj = JSON.parse(da)//将字符串转换为json对象
  13. form.val("rmdj", {
  14. //"输入框Name": json键值
  15. "card0": obj.kno
  16. , "SmartCardNumber": obj.SmartCardNumber
  17. });
  18. },
  19. error: function (e) {
  20. layer.alert("保存失败!请确认参数填写是否正确!");
  21. }
  22. });
  23. return false;
  24. });
  25. <script></script>
  26. </head>
  27. <body></body>
  28. </html>

JsonResult返回json格式的字符串,前端ajax接收后解析为json对象并对输入框进行赋值

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/861707
推荐阅读
相关标签
  

闽ICP备14008679号