使用mvc测试Demo
================view================
@{ ViewBag.Title="Index"; Layout=null; } <h2>ajax请求参数测试</h2> <scriptsrc="~/Scripts/jquery-1.8.2.js"></script> <div> <divid="div_Data"> <formid="_form"> <table> <tbody> <tr> <td><label>编号:</label></td> <td><inputtype="text"name="ID"value="2"/></td> </tr> <tr> <td><label>名称:</label></td> <td><inputtype="text"name="Name"value="测试人员"/></td> </tr> <tr> <td><label>列表:</label></td> <td><inputtype="text"name="ListStr"value="测试A,测试B"/></td> </tr> <tr> <td><label>日期:</label></td> <td><inputtype="text"name="Date"value="2015-12-1710:50:30"/></td> </tr> </tbody> </table> </form> </div> <inputid="inpBtn"type="button"value="点击事件+get"/> <inputid="inpBtn2"type="button"value="点击事件2+get"/> <inputid="inpBtn3"type="button"value="点击事件3+post"/> <inputid="inpBtn4"type="button"value="点击事件4+post"/> </div> <scripttype="text/javascript"> Array.prototype.toJson=function(){ varjson={}; for(variinthis){ varo=this[i]; if(o.name!='__VIEWSTATE'){ if(typeof(json[o.name])=='undefined') json[o.name]=o.value; else json[o.name]+=","+o.value; } } returnjson; } $('#inpBtn').click(function(){ debugger; var_formObj=$('#_form'); vararr=_formObj.serializeArray();//获取表单数据 for(vari=0,l=arr.length;i<l;i++){ if(arr[i].name=="ListStr"){ varlist=arr[i].value.split(','); deletearr[i]; for(varj=0,len=list.length;j<len;j++){ varitem={ name:"ListStr",value:list[j] }; arr.push(item); } break; } } $.ajax({ url:'/AjaxRequest/GetTestAjax',type:'get',data:arr,async:false,success:function(data,status){ debugger; },error:function(ex){ debugger; } }); }); $('#inpBtn2').click(function(){ debugger; var_formObj=$('#_form'); vararr=_formObj.serializeArray(); varjsonObj=arr.toJson(); jsonObj.ListStr=jsonObj.ListStr.split(','); varjson=JSON.stringify(jsonObj);//object转字符串 $.ajax({ url:'/AjaxRequest/GetTestAjax2',data:{'json':json},success:function(data){ debugger; },error:function(ex){ debugger; } }); }); $('#inpBtn3').click(function(){ debugger; var_formObj=$('#_form'); vararr=_formObj.serializeArray(); varjsonObj=arr.toJson(); jsonObj.ListStr=jsonObj.ListStr.split(','); varaaa=jsonObj.ListStr; //varlist=JSON.stringify(aaa); //varjson=JSON.stringify(jsonObj);//object转字符串 varpara={ test:jsonObj,list:aaa }; varparaStr=JSON.stringify(para); $.ajax({ url:'/AjaxRequest/GetTestAjax3',type:'post',contentType:"application/json",data:paraStr,error:function(ex){ debugger; } }); }); $('#inpBtn4').click(function(){ debugger; var_formObj=$('#_form'); vararr=_formObj.serializeArray(); varjsonObj=arr.toJson(); jsonObj.ListStr=jsonObj.ListStr.split(','); varjson=JSON.stringify(jsonObj);//object转字符串 $.ajax({ url:'/AjaxRequest/GetTestAjax4',data:json,error:function(ex){ debugger; } }); }); </script>
===============controller===============
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.Mvc; usingSystem.Web.Script.Serialization; namespaceContentReplace.Controllers { publicclassAjaxRequestController:Controller { // //GET:/AjaxRequest/ publicActionResultIndex() { returnView(); } publicJsonResultGetTestAjax(TestClasstest) { vardata=""; returnJson(data,JsonRequestBehavior.AllowGet); } publicJsonResultGetTestAjax2() { varjson=Request["json"]; varjss=newJavaScriptSerializer(); vararr=jss.Deserialize<TestClass>(json); vardata=""; returnJson(data,JsonRequestBehavior.AllowGet); } [HttpPost] publicJsonResultGetTestAjax3(TestClasstest,List<string>list) { vardata=""; returnJson(data,JsonRequestBehavior.AllowGet); } [HttpPost] publicJsonResultGetTestAjax4(TestClasstest) { vardata=""; returnJson(data,JsonRequestBehavior.AllowGet); } } publicclassTestClass { publicintID{get;set;} publicstringName{get;set;} publicDateTimeDate{get;set;} publicList<string>ListStr{get;set;} } }原文链接:https://www.f2er.com/ajax/162586.html