【发布时间】:2011-11-28 07:31:23
【问题描述】:
亲爱的,我想知道如何使用 jquery ajax 功能提交 asp.net 表单数据以及我如何在服务器端获取该 json 数据
?
【问题讨论】:
-
亲爱的你...... 这不是生成代码论坛,向我们展示你尝试了什么,你测试了什么,然后我们如果您遇到困难,将帮助您编写一些代码...
亲爱的,我想知道如何使用 jquery ajax 功能提交 asp.net 表单数据以及我如何在服务器端获取该 json 数据
?
【问题讨论】:
<form action="#" id="example-form">
...
</form>
<script type="text/javascript">
$('#example-form').submit(function(){
// block form
$.post('putUrlHere', $('#example-form').serialize(), function(data) {
//call back happens here. Unblock form/show result
});
return false; // this prevents regular post, forcing ajax post.
});
</script>
参考: http://forums.asp.net/t/1611677.aspx/1?Jquery+with+ASP+NET+Form+submit
否则检查:jquery + ajax 结合..... http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
【讨论】:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$("#<%=btnSubmit.ClientID %>").click(
function()
{
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/AcceptFormData",
data: "{'funcParam':'"+$('#formName').serialize()+"'}",
dataType: "json",
success: function(msg)
{
var msgFromASPXFunction = msg.d
}
});
}
);
});
[System.Web.Services.WebMethod]
public static string AcceptFormData(string funcParam)
{
// this is your server side function
//the data you will get will be in format e.g. a=1&b=2&c=3&d=4&e=5 inside funcParam variable
return "Data Submitted";
}
请注意,服务器端函数名称应与您在 jQuery 的 $.ajax 函数的 url 属性中指定的参数匹配。 (在上述情况下,它是“AcceptFormData”
参数名称也应与服务器端函数参数的名称相同。在这种情况下,它是“funcParam”。
只需比较这段代码中的 $.ajax 和服务器端函数即可。
问候,
阿明·赛义德
(注:如果有帮助请标记)
【讨论】: