路由在 Angular 和 nodejs 应用程序中不起作用

Routing is not working in an Angular and nodejs application

本文关键字:应用程序 不起作用 nodejs Angular 路由      更新时间:2023-09-26

基本上我不是JS开发人员。但是对于应用程序,我使用angularjs作为前端,nodejs作为后端。

在angularjs中,我编写了一个需要由用户填写的表单,一旦用户填写了该表单,他就可以使用提交按钮提交。在提交按钮单击事件时,我在下面编写了代码。

<div class="submit" align="center">
    <input type = "submit" value="Submit !" ng-click="addResume()">
 </div>

其中 addResume() 是在角度控制器中声明的函数,如下所示 -

$scope.addResume = function(){
    console.log($scope.resume);
    $http.post("/Resume", $scope.resume);
};

此函数将在节点 js 服务器上调用函数。这将简单地在 success.html 页面上打印请求字段和路由。请参阅下面的代码 -

 app.post('/Resume',function(req,res, next){
    console.log(req.body);
     // res.render(path.join(__dirname)+'/views/success.html');
    next();
 })

现在我面临的问题是我能够通过角度表单提交数据,但应用程序没有移动到成功页面,我尝试了选项 res.render 和 next(),但无法流向成功页面。

请帮助我解决这个问题。

您执行 Ajax 请求时,ajax 请求不会加载您重定向的页面。

如果你想在下一页移动,你必须从客户端进行:

$http.post("/Resume", $scope.resume).then(function(response){
    // success move to next page here
}, function(rejection){
    // handle failure
})

另一种方法是调用提交表单函数,以便表单以本机方式提交,但数据不会作为 json 发送到服务器,它们将使用请求正文中的经典查询字符串格式发送。