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

一、传变量

客户端请求

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

javascript 复制代码
 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}`);

 }

服务器端

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

csharp 复制代码
 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流数据。

javascript 复制代码
 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数据是按流来处理的,所以就必须从流中读取,并自动进行解析。

csharp 复制代码
 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);
  }
相关推荐
wdfk_prog15 分钟前
LWIP教程 09:从 `tcp_slowtmr()` 到 Fast Retransmit——TCP 超时重传与重复 ACK
服务器·网络·数据库·网络协议·tcp/ip
‎ദ്ദിᵔ.˛.ᵔ₎16 分钟前
linux 进程信号 (上)
linux·服务器
迪康Defender29 分钟前
终端安全审批体系设计:如何平衡安全管控与业务连续性
运维·服务器·安全·运维开发·学习方法·业界资讯·远程工作
liulilittle1 小时前
Linux 下 select 测试函数
linux·服务器·网络·数据库·c++·select·c
luo_guibin2 小时前
Linux系统下OpenSSL升级全流程,新旧版本切换
linux·运维·服务器
看着博客敲代码2 小时前
No space left on device:服务器被 SSH 爆破到日志写满的完整排查与止血
linux·运维·服务器·安全·ssh
极光通讯2 小时前
服务器内存来料检验(IQC)实操:批次追溯、外观判定与上机验证
java·服务器·算法
Raspberry_Pi_官方账号2 小时前
Raspberry Pi Desktop 全面升级,现已登陆 PC 和 Mac
服务器·网络·macos·树莓派·raspberry pi·raspberrypios
Rooting++3 小时前
用友打包的APP壳子怎么获取SHA1码?
运维·服务器