ASP.NET MVC3中关于httppost提交 (转)
一、正常的post提交
Controller
[HttpPost] public string post_test(string str) { return "post的字符串是:"+str; }
VIEW
<input id="btn_test" type="button" value="测试" onclick="post_test();" /> <label id="lbl_show"></label> <script type="text/javascript"> function post_test() { $.post("/test/post_test", { str: "John" }, function (data) { $('#lbl_show').text(data); }); } </script>
这个是正常的post提交

二、使用$.post(),但在url后加参数提交
修改VIEW
<input id="btn_test" type="button" value="测试" onclick="post_test();" /> <label id="lbl_show"></label> <script type="text/javascript"> function post_test() { $.post("/test/post_test?str=John", function (data) { $('#lbl_show').text(data); }); } </script>

可见这个与get方法提交是没什么区别的。
三、同名参数提交
<input id="btn_test" type="button" value="测试" onclick="post_test();" /> <label id="lbl_show"></label> <script type="text/javascript"> function post_test() { $.post("/test/post_test?str=Jim", { str: "John" }, function (data) { $('#lbl_show').text(data); }); } </script>

可见url后提交的参数被忽略了。
四、参数名不同
[HttpPost]
public string post_test(string str1,string str2)
{
return "post的字符串是:"+str1+","+str2;
}
<input id="btn_test" type="button" value="测试" onclick="post_test();" /> <label id="lbl_show"></label> <script type="text/javascript"> function post_test() { $.post("/test/post_test?str1=Jim", { str2: "John" }, function (data) { $('#lbl_show').text(data); }); } </script>
结果:

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

浙公网安备 33010602011771号