使用AJAX将数据从HTML表单提交到WebMethod

编程入门 行业动态 更新时间:2024-10-27 22:28:41
本文介绍了使用AJAX将数据从HTML表单提交到WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我要从HTML表单中获取数据,然后使用AJAX将数据发送到Web方法,然后再发送到sqlite数据库,但是我的AJAX调用失败.我搞砸了什么?我做得对吗?

So I'm taking in data from an HTML form and then using AJAX to send the data to a web method to then be sent to a sqlite database, but my AJAX call is failing. What did I mess up? Am I doing it correctly?

HTML表单

<form id="addForm" > <input type="text" name="playername" id="playername" placeholder="Player"/> <input type="text" name="points" id="points" placeholder="Points" /> <input type="text" name="steals" id="steals" placeholder="Steals" /> <input type="text" name="blocks" id="blocks" placeholder="Blocks" /> <input type="text" name="assists" id="assists" placeholder="Assists" /> <input type="text" name="mpg" id="mpg" placeholder="MPG" /> <input type="text" name="shotpct" id="shotpct" placeholder="Shot %" /> <input type="text" name="threepct" id="3pct" placeholder="3 %" /> <input type="button" value="add player" id="addbtn" name="addbtn" /> </form>

AJAX

$("#addbtn").click(function () { var form = $("#addForm").serializeArray(); $.ajax({ type: 'POST', url: "players.aspx/addRow", data: JSON.stringify(form), dataType: 'json', success: function () { alert('success'); }, error: function () { alert('failure'); } }); });

和网络方法(尚未完成,只是在测试以查看我是否正在获取数据)

and the web method(not finished, was just testing to see if I was getting data)

[WebMethod] public static void addRow(object form) { var stuff = form; }

我仍在学习如何使用大量此类内容,因此将不胜感激任何帮助.

I'm still learning how to use a lot of this stuff so any help will be greatly appreciated.

推荐答案

替换

type: 'POST',

使用

method: 'POST',

dataType:"json" 不需要,因为您没有收到数据.从服务器返回的数据将根据dataType参数进行格式化.

dataType: 'json' is not needed since you're not receiving data back. The data returned from the server, is formatted according to the dataType parameter.

还删除JSON.stringify(form),这已经通过.serialize()完成了;

Also remove JSON.stringify(form),this is already done with the .serialize();

更多推荐

使用AJAX将数据从HTML表单提交到WebMethod

本文发布于:2023-10-09 17:56:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476380.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   数据   AJAX   WebMethod   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!