.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);
  }
相关推荐
脏脏a18 小时前
【Linux】进程优先级:谁先 “上车” 谁说了算?
linux·运维·服务器
_星辰大海乀1 天前
IP 协议
服务器·网络·tcp/ip·nat·子网掩码·ip协议
屿行屿行1 天前
【Linux】Socket编程(基于实际工程分析)
linux·服务器·网络
runepic1 天前
Python + PostgreSQL 批量图片分发脚本:分类、去重、断点续拷贝
服务器·数据库·python·postgresql
企鹅侠客1 天前
Linux性能调优 详解磁盘工作流程及性能指标
linux·运维·服务器·性能调优
企鹅侠客1 天前
Linux性能调优 再谈磁盘性能指标和进程级IO
linux·运维·服务器·性能调优
虚伪的空想家1 天前
云镜像,虚拟机镜像怎么转换成容器镜像
服务器·docker·容器·k8s·镜像·云镜像·虚机
最笨的羊羊1 天前
Flink CDC系列之:Kafka 变更日志 JSON 格式工厂类 ChangeLogJsonFormatFactory
json·flink cdc系列·changelog·kafka 变更日志·json 格式工厂类·formatfactory
在路上@Amos1 天前
Linux 命令行查看 串口hex数据
linux·运维·服务器
人工智能训练1 天前
Linux 系统核心快捷键表(可打印版)
linux·运维·服务器·人工智能·ubuntu·容器·openeuler