使用 jQuery 将表单发布到 Web 方法变得非常困惑
我有一个包含相当多值的表单。我目前正在使用 POST 方法提交,但我希望将其转换为通过 AJAX 使用 jQuery 来完成。
我一直在尝试将代码隐藏的“保存到数据库”功能转换为 Web 方法。我使用两步函数方法,首先准备 Request.Form("parameterName") 中的所有变量,然后将它们全部传递到“saveData”函数中,如下所示:
<System.Web.Services.WebMethod()> _
Public Shared Sub prepareRequestFormValues()
/// save to database and do stuff with the variables
variableName = HttpContext.Current.Request("parameterName")
saveToDatabase(list, of, form, variables, ...)
End Sub
读完这篇文章 将表单序列化到 asp.net webmethod
我可以看到使用 jQuery 表单插件我可以序列化整个表单并将其作为单个变量发送到处理代码。
然而,我现在已经完全困惑了。鉴于我对 AJAX/codebehind 等还很陌生,我一直在兜圈子,现在很困惑!
使用
var queryString = $('input').fieldSerialize();
$.ajax({
type: "POST",
url: "Detail.aspx/doPostTest",
data: "{ 'txtQuery': '" + queryString + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var products = eval(data.d);
console.log(products);
}
});
从上面提到的帖子,我可以看到我可以发送这个字符串,但是收到后如何在服务器端处理它?该文章暗示 HttpContext.Current.Request.Form("parameterName")
应该可以工作,但事实并非如此......我猜是因为我需要从 txtQuery 变量访问它。 ..但是...我恐怕迷路了!
I have a form which contains a fairly large number of values. I am currently submitting with a POST method, but I wish to convert this to do it with jQuery via AJAX.
I have been experimenting with converting my code-behind "save to database" function into a Web Method. I am using a two-step function method, firstly preparing all the variables from Request.Form("parameterName") and then passing them all into a "saveData" function as follows:
<System.Web.Services.WebMethod()> _
Public Shared Sub prepareRequestFormValues()
/// save to database and do stuff with the variables
variableName = HttpContext.Current.Request("parameterName")
saveToDatabase(list, of, form, variables, ...)
End Sub
Having read this post Send form serialize to asp.net webmethod
I can see that using the jQuery forms plugin I can serialize the whole form and send it as a single variable to the processing code.
However, I have got myself completely confused now. Given that I am pretty new to AJAX/codebehind and so on I've been going round in circles and now am very confused!
Using
var queryString = $('input').fieldSerialize();
$.ajax({
type: "POST",
url: "Detail.aspx/doPostTest",
data: "{ 'txtQuery': '" + queryString + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var products = eval(data.d);
console.log(products);
}
});
From the post mentioned above, I can see I can send this string, but how do I process it server-side when recieved? The article implied that HttpContext.Current.Request.Form("parameterName")
should work, but it doesn't... I'm guessing because I need to access it from the txtQuery variable.... but.... I'm lost I am afraid!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个非常好的教程 包含执行此操作的 JS。不要发布到 PHP,而是发布到您的 aspx 页面。这是有类似问题的论坛帖子。 XIII发布了以下代码。
还有你的WebMethod
Here is a really good tutorial containing the JS to do this. Instead of posting to PHP post to your aspx page. Here is an forum post with a similar question. XIII had posted the follwoing code.
And your WebMethod