【发布时间】:2015-12-17 20:19:18
【问题描述】:
我想用用户填写的 html 表单中的数据发送 post 请求。我想使用 jquery 来实现这一点。这是我的尝试(不工作):
$(function () {
$("#create").click(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "/home/new",
data: $(this).serialize(),
success: function (data, textStatus, jqXhr) {
//call "home/new" with data from html form as json and update current view with returned data
console.log("success");
},
error: function () {
alert("error");
}
});
});
});
<html lang="en">
<head>
<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/Helpers.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Title Goes Here</title>
</head>
<body>
<form>
Note:<br>
</form>
<textarea rows="4" cols="50" name="note" form="form">
</textarea>
<br/>
<input type="date" name="day" form="form">
<input type="submit" id="create" value="Submit" form="form">
</body>
</html>
【问题讨论】:
-
$(this)在这种情况下 not 是否指的是表单,您为什么期望这样?你可以自己解决,绝对。开始使用您的浏览器开发控制台并输出$(this)。查看对象... -
对,我将使用控制台。谢谢,