前端往后端提交數據的方式常用的就這么三種: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獲取即可,非常簡單