Javascript 通过jquery提交表单时没有ajax post请求
我正在尝试向服务器提交我的表单。我已经使用帕格创建了我的表单,然后通过ajax提交它。我已经编写了一个测试控制台日志,以查看表单是否正在提交,消息是否没有被记录。出了什么问题 然而,无论我在表单上输入什么,都会在?之后出现在url中?。还有,为什么没有提出post请求 我的表格:-Javascript 通过jquery提交表单时没有ajax post请求,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在尝试向服务器提交我的表单。我已经使用帕格创建了我的表单,然后通过ajax提交它。我已经编写了一个测试控制台日志,以查看表单是否正在提交,消息是否没有被记录。出了什么问题 然而,无论我在表单上输入什么,都会在?之后出现在url中?。还有,为什么没有提出post请求 我的表格:- form(id="newProductForm") p label(for="title") Product Title: in
form(id="newProductForm")
p
label(for="title") Product Title:
input(type="text" name="title" id="title" placeholder="Product Title" required autofocus)
p
label(for="description") Product Description:
input(type="text" name="description" id="description" placeholder="Product Description" required autofocus)
p
label(for="Price") Price:
input(type="price" name="price" id="price" placeholder="Set Price" required autofocus)
p
label(for="Price") Shipping Charges:
input(type="text" name="shippingCharge" id="shippingCharge" placeholder="Set Shipping Charges" required autofocus)
p
label(for="Price") Shipping Time:
input(type="text" name="shippingTime" id="shippingTime" placeholder="Set Shipping Time" required autofocus)
p
label(for="quantity") Total Quantity:
input(type="text" name="quantity" id="quantity" placeholder="Set Total Quantity" required autofocus)
p
label(for="tags") Tags (Seperated by comma):
input(type="text" name="tags" id="tags" placeholder="Set Price" required autofocus)
//Add drag and drop image upload feature soon
p
button(type="submit" id="addProductButton") Add New product
提交表单的我的JS代码:-
$(document).ready( function () {
getAllProducts();
$('#newProductForm').submit( function(e) {
e.preventDefault();
let arr = $(this).serializeArray();
console.log("Form values: "+ arr);
$.post({
url: '/admin/add',
type: 'POST',
}).done( response => {
resetProductTable();
console.log("Admin add is being requested!")
})
})
})
您的提交按钮定义为
我相信它应该是一个
,你可以根据自己的喜好将它设计成一个按钮:
p
input(type="submit" id="addProductButton") Add New product
您的提交按钮定义为
我相信它应该是一个
,你可以根据自己的喜好将它设计成一个按钮:
p
input(type="submit" id="addProductButton") Add New product
我认为你的表格提交不正确? 你可以用这种方法,我认为你的问题应该解决它
$(document).submit('#newProductForm'', function (e) {
)}
我认为你的表格提交不正确? 你可以用这种方法,我认为你的问题应该解决它
$(document).submit('#newProductForm'', function (e) {
)}
将触发提交事件更改为
$(document).on('click', '#addProductButton', ()={
...
}):
这将直接用sumbit按钮绑定事件。将触发提交事件更改为
$(document).on('click', '#addProductButton', ()={
...
}):
这将直接用sumbit按钮绑定事件。您的意思是您的函数被post调用,而不是被请求?你在开发者控制台的网络选项卡上看到了什么?控制台上没有任何内容。不过,我可以看到我输入的数据记录在url区域。我有点奇怪,你的意思是你的功能是通过邮件调用的,而不是请求?你在开发者控制台的网络选项卡上看到了什么?控制台上没有任何内容。不过,我可以看到我输入的数据记录在url区域。我发现在函数顶部添加
e.preventDefault()
。在函数顶部添加e.preventDefault()
。这很有效。谢谢!我可以知道是什么导致了问题吗?是的,当然,我们正在提交表单,但实际上加载了我们的文档。文档意味着整个页面,你明白吗?这很有效。谢谢!我可以知道是什么导致了问题吗?是的,当然,我们正在提交表单,但实际上加载了我们的文档。文档是指整个页面,明白吗?