【发布时间】:2021-05-13 00:35:42
【问题描述】:
我有一个简单的表单,我试图通过 ajax 提交。我在 Jquery 中加载并得到了这个简单的表单:
<form id="post_edit">
<input type="hidden" id="id" name="id" value="1">
<input type="text" id="title" name"title" value="This is the title">
<textarea id="body" name="body" rows="3">This is the body</textarea>
<button type="submit" onclick="this.form.submit();" data-dismiss="modal">Save</button>
然后我有这段代码通过 ajax 提交表单:
$("#post_edit").submit(function(e) {
e.preventDefault();
var form = $(this);
var url = 'http://localhost/ajax/posts/' + form.attr('id');
$.ajax({
type: "PATCH",
url: url,
data: form.serialize(), // serializes the form's elements.
success: function(data)
{
toastr.success('The post was successfully updated!')
},
error:function() {
toastr.error('Unable to save changes to the post.<br />Please try again later.')
}
});
});
不知道为什么,但是 ajax 提交不起作用,而只是 POST 到当前 URL。
【问题讨论】: