ASP.NET MVC3 httppost写法的一些对比

时间:13-06-03 栏目:MVC 作者:zongyan86 评论:0 点击: 5,690 次

一、正常的post提交

Controller

  1. [HttpPost]  
  2. public string post_test(string str)  
  3. {  
  4.     return "post的字符串是:"+str;  
  5. }  

VIEW

  1. <input id="btn_test" type="button" value="测试" onclick="post_test();" />  
  2. <label id="lbl_show"></label>  
  3. <script type="text/javascript">  
  4.     function post_test()  
  5.     {  
  6.         $.post("/test/post_test", { str: "John" }, function (data) {  
  7.             $('#lbl_show').text(data);  
  8.         });  
  9.     }  
  10. </script>  

这个是正常的post提交

二、使用$.post(),但在url后加参数提交

修改VIEW

  1. <input id="btn_test" type="button" value="测试" onclick="post_test();" />  
  2. <label id="lbl_show"></label>  
  3. <script type="text/javascript">  
  4.     function post_test()  
  5.     {  
  6.         $.post("/test/post_test?str=John", function (data) {  
  7.             $('#lbl_show').text(data);  
  8.         });  
  9.     }  
  10. </script>  

可见这个与get方法提交是没什么区别的。

三、同名参数提交

  1. <input id="btn_test" type="button" value="测试" onclick="post_test();" />  
  2. <label id="lbl_show"></label>  
  3. <script type="text/javascript">  
  4.     function post_test()  
  5.     {  
  6.         $.post("/test/post_test?str=Jim", { str: "John" }, function (data) {  
  7.             $('#lbl_show').text(data);  
  8.         });  
  9.     }  
  10. </script>  

可见url后提交的参数被忽略了。

四、参数名不同

  1. [HttpPost]  
  2. public string post_test(string str1,string str2)  
  3. {  
  4.     return "post的字符串是:"+str1+","+str2;  
  5. }  

  1. <input id="btn_test" type="button" value="测试" onclick="post_test();" />  
  2. <label id="lbl_show"></label>  
  3. <script type="text/javascript">  
  4.     function post_test()  
  5.     {  
  6.         $.post("/test/post_test?str1=Jim", { str2: "John" }, function (data) {  
  7.             $('#lbl_show').text(data);  
  8.         });  
  9.     }  
  10. </script>  

结果:

可见str1,str2的值都传入了,str1使用的是get方式,str2使用的是post方式

原文地址:http://blog.csdn.net/shujudeliu/article/details/8570997



声明: 本文由( zongyan86 )原创编译,转载请保留链接: ASP.NET MVC3 httppost写法的一些对比

关注我们