C#后端接收前端的各種類型數據


 前端往后端提交數據的方式常用的就這么三種:1.form提交;2.url參數提交;3.json提交

1.針對表單form方式的提交

在后端使用Request.Form的方式接收,比如

   前端代碼片段:

 var businesstypes = $("#businesstypes").val();
 if (businesstypes == null || businesstypes == '') return;
 var value = $("form").serialize();
 $.post('@Url.Action("BatchPublish")', value, function (data) 
{
  ....
} 

  后端代碼片段:

 FormCollection form = new FormCollection(Request.Unvalidated().Form);
 string businestypes = form["businesstypes"];

  2.針對json的情況

前端代碼:

var rst = JSON.stringify(object xxx);
$.post(posturl, rst, function (data) {...}

后端代碼:

using (StreamReader stream = new System.IO.StreamReader(Request.InputStream))
{
string Jsonobj = stream.ReadToEnd();
var MeEntity = Newtonsoft.Json.JsonConvert.DeserializeObject<MenuEntity>(Jsonobj);
}

  3.針對Url里面的參數,這個一般是用在Get上。上面的幾種是說的POST的情況;

Get的方式使用Request.QueryString獲取即可,非常簡單


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM