[英]How to send data with ajax, without reloading page? (Nodejs)
I can't send my data and save it in database, without reloading. 我无法发送数据并将其保存在数据库中,而无需重新加载。 I Can't even understand, why my page reloading. 我什至不明白,为什么我的页面重新加载。 I have no idea, how I can solve my problem... 我不知道如何解决我的问题...
Here is my form: 这是我的表格:
<form action="/user/change" method="post">
Name: <input type="text" id="name" name='profile_name' value="{{myName}}">
Surname: <input type="text" id="surname" name="profile_surname" value="{{mySurname}}">
Age: <input id="profile_age" name="age" type="text" value="{{myAge}}">
<input type="hidden" name="_csrf" value='{{csrfToken}}'>
<input type="submit" value="Send" id="send_profile">
</form>
Here is my ajax script: 这是我的ajax脚本:
$('#send_profile').click(function() {
$.ajax({
global: false,
type: 'POST',
url: /user/change,
dataType: 'html',
data: {
name: $("#profile_name").val(),
surname: $("#profile_surname").val(),
age: $("#profile_age").val()
},
success: function (result) {
console.log(result);
},
error: function (request, status, error) {
serviceError();
}
});
});
And my Node.js request handler: 还有我的Node.js请求处理程序:
router.post('/change', function (req, res) {
var name = req.body.name,
surname = req.body.surname,
age = req.body.age
User.findOneAndUpdate({_id: req.user._id}, {$set:{name: name, surname: surname, age: age}}, {new: true}, function(err, doc){
if(err){
console.log("Something wrong when updating data!");
}
console.log(req);
console.log('req received');
res.redirect('/user/'+req.user._id);
});
});
On submit By default your form data will send on action attribute of your form. 提交时默认情况下,您的表单数据将发送表单的action属性。 if it is empty them it will send data on the same page. 如果为空,它们将在同一页上发送数据。 for preventing the default functionality of submit of any form we use e.preventDefault();
为了防止使用任何形式的默认提交功能,我们使用e.preventDefault();
$('#send_profile').click(function() {
e.preventDefault();
$.ajax({
global: false,
type: 'POST',
url: '/user/change', // missing quotes
dataType: 'html',
data: {
name: $("#profile_name").val(),
surname: $("#profile_surname").val(),
age: $("#profile_age").val()
},
success: function (result) {
console.log(result);
},
error: function (request, status, error) {
serviceError();
}
});
});
Your page is reloading because your are submitting it with input type=submit. 您的页面正在重新加载,因为您正在使用输入type = submit提交页面。
Replace that with a regular button and it should work. 用常规按钮替换它,它应该可以工作。
Edit: you can remove the post from the form tag as well. 编辑:您也可以从表单标签中删除帖子。
As noted above the issue stems from using a form and a submit button, one solution is to remove the form and submit and just pull the data from the onClick handler. 如上所述,问题源于使用表单和提交按钮,一种解决方案是删除表单并提交并仅从onClick处理程序中提取数据。 I personally prefer to leave a working form, just in case a person has JS disabled (rare, but possible) the form will still submit with a reload. 我个人更喜欢留下一个有效的表格,以防万一某个人禁用了JS(很少见,但有可能),表格仍然会重新加载。
This method requires you prevent default behavior for the click event, I've added a simple event.preventDefault()
call before your jQuery AJAX handler. 此方法要求您防止click事件的默认行为,我在jQuery AJAX处理程序之前添加了一个简单的event.preventDefault()
调用。 It will prevent the default behavior of the event (in this case submitting the form via post) allowing your handler to do that without reloading the page. 这将防止事件的默认行为(在这种情况下,是通过邮寄提交表单),使您的处理程序无需重新加载页面即可这样做。
More info on the event object 有关事件对象的更多信息
$('#send_profile').click(function(event) {
event.preventDefault();
$.ajax({
global: false,
type: 'POST',
url: /user/change,
dataType: 'html',
data: {
name: $("#profile_name").val(),
surname: $("#profile_surname").val(),
age: $("#profile_age").val()
},
success: function (result) {
console.log(result);
},
error: function (request, status, error) {
serviceError();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.