gpt4 book ai didi

javascript - 为什么 Node 应用程序中的 HTTP POST 请求在提交表单数据时需要刷新

转载 作者:行者123 更新时间:2023-12-01 03:00:34 25 4
gpt4 key购买 nike

Angular 代码

.state('studentInfo.newStudent', {
url : '/new/student',
templateUrl: 'students/new-student.html',
controller : function($state, $http){
this.saveStudent = function(student){
$http({method: 'POST', url: `/studentInfo/new`, data: {student}}).then(function(){
$state.go('studentInfo');
});

快速代码

app.post('/studentInfo/new', jsonParser, (request, response)=>{
let students = mongoUtil.students();
let newStudent = request.body.student;
students.save(newStudent, (err, res) =>{
if(err){
res.sendStatus(400);
}
res.sendStatus(201);
});
});

我的问题是这些代码用于在提交 Angular 表单时保存数据。但需要刷新才能看到我的更新

http://localhost:8181/#!/studentinfo

我仍然可以提供一个状态来继续“studentinfo”。需要进行哪些更改才能更新“studentinfo”路径中的列表以查看我当前保存的数据而无需刷新浏览器

最佳答案

如果您在以 json 形式发送响应和数据时返回数据,并使用 Angular 解析它并显示在 View 上。

成功的情况下,发送201响应时必须返回newstudent变量。

如果您想让我编写代码,请在我的答案下面的评论中提及

关于javascript - 为什么 Node 应用程序中的 HTTP POST 请求在提交表单数据时需要刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46452346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com