欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > .net 如何处理网页的Json请求?

.net 如何处理网页的Json请求?

2025/7/3 17:08:30 来源:https://blog.csdn.net/m0_37946533/article/details/145345633  浏览:    关键词:.net 如何处理网页的Json请求?

一、传变量

客户端请求

客户端一般是用Jscript写的,传送变量,代码如下:

 function save(i){//这是用Post Method 使用var1=value1&var2=value2格式传送数据  var count=chkCount.value;var chk0="";for (k=0;k<count;k++){var chkBox=document.getElementById("c_"+k.toString());if (chkBox.checked){chk0+=chkBox.value+",";}}var s0 = "/Store/Save" + i.toString() ;                xhr0.open("POST", s0, true);xhr0.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xhr0.send(`chk0=${chk0}`);}

服务器端

按变量进行接收,并已解析完毕。

 public async Task<JsonResult> Save1(string chk0)      {int n = 0;string[] sa=chk0.Split(",");List<string> chk =new List<string>(sa);chk.RemoveAt(chk.Count-1); */          if (chk0.Length > 0){n = await SaveChk(chk0, 1);}return RedirectToAction(nameof(Warehousing));
}

二、传Json数据

客户端

传送Json流数据。

 var count=chkCount.value;var chk0=[];var chkBox;for(n=0;n<count;n++){chkBox=document.getElementById("c_"+n.toString());if (chkBox.checked){chk0[chk0.length]=chkBox.value;}}var s0 = "/Store/Save" + i.toString() ;xhr0.open("POST", s0, true);xhr0.setRequestHeader("Content-type","application/Json");//"application/x-www-form-urlencoded");// ;xhr0.send(JSON.stringify(chk0));

服务器端

服务器端,对Json数据是按流来处理的,所以就必须从流中读取,并自动进行解析。

 public async Task<JsonResult> Save1()      {string[] chk = await Request.ReadFromJsonAsync<string[]>(); ;if (chk.Length > 0){n = await SaveChk(chk, 1);}int status = 0;int flowId = 3;List<WaybillView> waybillViews = GetWaybillViews(status, flowId);return Json(waybillViews);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词