【问题标题】:Sending Data from Angular Front End to NodeJS in a json file在 json 文件中从 Angular 前端向 NodeJS 发送数据
【发布时间】:2017-04-27 11:14:03
【问题描述】:

我目前正在尝试将 AngularJS 前端连接到 NodeJS 后端应用程序。理想的目标是从 json 文件 而非数据库中读取、写入、编辑和删除数据。 我试过在两边都设置 post 方法,但它说我的 json 包含信息的文件是404

在创建前端之后,我之前从未实施过后端,所以我很不确定如何进行。

这是我的 Angular 控制器:

var app = angular.module('app', []);

app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

app.controller('dashCtrl', function($scope, $http){

   $http.get('/Alpha/json/details.json').then(function (response) {
        $scope.details = response.data;
        console.log(details);
    });

   $scope.newDetails = {};

    $scope.selectItem = function(item){
        console.log(item);
        $scope.clickedDetails = item;
    };

    $scope.updateItem = function(){

    };

    $scope.deleteItem = function(){
        $scope.details.splice($scope.details.indexOf($scope.clickedDetails), 1);
    };

   $scope.saveItem = function(){        
        $scope.details.push({ 'id':$scope.id, 'system': $scope.system, 'date':$scope.date, 'priority':$scope.priority, 'description':$scope.description, 'status':$scope.status });
        // Writing it to the server
        //      
        var item = {
            id:$scope.id, 
            system: $scope.system, 
            date:$scope.date, 
            priority:$scope.priority, 
            description:$scope.description, 
            status:$scope.status
        };  

        $http.post('../json/details.json', item).then(function(item, status) {
            console.log('Data posted successfully');
        });

        // Making the fields empty
        //
        $scope.id = ''; 
        $scope.system = '';
        $scope.date = '';
        $scope.priority = '';
        $scope.description = '';
        $scope.status = ''; 
    };

});

我在后面跑的 NodeJS:

var express = require('express');
var app = express();
var bodyParser  = require('body-parser');

app.use(express.static(__dirname + '/'));

app.post('/Alpha/pages/json/details.json', function(req, res) {
    console.log(details);
    res.end();
});

app.listen(3000);

我的 HTML 表单:

<table class="table table-responsive">

    <thead style="background-color:#002244; color:#FFFFFF">
        <tr>
            <th> Issue </th>
            <th> System </th>
            <th> Date </th>
            <th > Priority </th>
            <th > Description </th>
            <th > Status </th>
            <th style="background:white;">
                <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" value="Add +" style=" border-color:#FFBE00; color: black;background-color:#FFBE00;">Add</button>
            </th>
        </tr>
    </thead>
    <tbody> 
        <tr ng-repeat=" item in details ">
            <td > {{item.id}}</td>
            <td > {{item.system}} </td>
            <td > {{item.date}}</td>
            <td class="{{item.priority}}-priority"> {{item.priority}} </td>
            <td > {{item.description}}</td>
            <td > {{item.status}} </td>
            <td>
                <a href ="#" data-toggle="modal" data-target="#myModalDelete" ng-click="selectItem(item)">Delete</a>
                <a href ="#" data-toggle="modal" data-target="#myModalEdit" ng-click="selectItem(item)">Edit</a>
            </td>
        </tr>
     </tbody>
</table>

为什么我在尝试发布到我的 json 文件时收到 404?如何从 NodeJS 中的 post 方法中指定使用 Angular 中的方法?

【问题讨论】:

  • 我认为你需要给出完整的路径而不是../json/details.json
  • Dev ToolsNetwork 选项卡中,右键单击details.json 调用(您将收到404 错误)并单击Open in new tab,并确保地址栏中显示的路径是有效的。
  • @DavidR 我可以正常访问 json 文件,但它一直给我一个 404
  • 您使用的是$http.get(..),而在您的后端,您期望一个发布请求,因为app.post(...) 要么让他们俩都获得请求,要么发布请求

标签: javascript angularjs json node.js


【解决方案1】:

我认为你的问题是你把文件名和扩展名放在你的 API 中....trey 给出端点 URL...例如:

在 Angular 类中:

 $http.post('../Alpha/pages/json/', item).then(function(item, status) {
            console.log('Data posted successfully');
        });

在你的 Node.js 中

   app.post('/Alpha/pages/json/', function(req, res) {
        console.log(details);
        res.end();
    });

【讨论】:

  • 遗憾的是,这并没有解决我遇到的问题。
  • 抱歉,json 在文件中?还是从您的后端以编程方式生成?
  • json 在我正在使用的静态文件中。
  • 啊啊好吧...那么它存储在后端吗? ..还是在前端目录中? ..
  • 存储在前端。我使用的唯一“后端”脚本就是这个小的 nodejs 脚本。我编写它只是为了添加、删除、编辑前端显示的数据。我不能为此使用数据库,而是使用静态文件。
猜你喜欢
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 2020-08-31
  • 2023-03-30
  • 2020-05-29
  • 2016-12-27
  • 1970-01-01
  • 2014-01-07
相关资源
最近更新 更多