javascript - Post 请求未通过 Angular.js 发送。我究竟做错了什么?

标签 javascript angularjs node.js model-view-controller http-post

所以我尝试从客户端单击获取一个名称,并将该名称(我将其更改为 json,以便实际上能够发送信息)发送到服务器。现在服务器将其发送回,客户端将其发布到索引页(我这样做只是为了可以看到服务器正在获取的数据)。当我看到数据时,它显示为[对象对象]。所以出了点问题,我希望有人能帮助我。

index.html

<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 
<table style="width:100%">
 <tr ng-repeat="contact in databases.databases">
    <td>{{ contact.name }} <button type="button"ng-click="addContactlist(contact.name)">Click Me</button></td>
    <td>{{ contact.sizeOnDisk }} </td>
    <td>{{ contact.empty }} </td>
  </tr>
  </table> 

DB name clicked: <p ng-bind="DB_NAME"></p>
</div>

</body>

客户端.js

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

app.controller('customersCtrl', function($scope, $http) {
    console.log("controller connected");

function refresh(){ 
// create contacklist route 
$http.get('/databases').success(function(response) {
    console.log("recived data requested");
    $scope.databases = response; 
  });
}

// Call refresh to init cantacklist 
refresh(); 


// add Doc to table 
$scope.addContactlist = function(contactItem) {
  alert("Working ");
    $http.post('/collection', JSON.stringify({'contactItem': contactItem})).success(function(response) {
      $scope.DB_NAME = response ; 
      });
    console.log("posted: "+contactItem);
  };




});// Controller 

服务器.js

var express = require('express');
var path = require('path'); //core module 
var databaseUrl = "localhost:27017/DB"; // default env
var bodyParser = require('body-parser');

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    assert = require('assert');

//configure app
var app = express(); 
var db = new Db('DB', new Server('localhost', 27017));


db.on('error', function (err) {
    console.log('database error', err)
}); 

db.on('connect', function () {
    console.log('database connected')
}); 

// store all html files in views 
app.use(express.static(__dirname + '/views'));
// parses recived json input 
app.use(bodyParser.json());
// store all js in Scripts folder
app.use(express.static(__dirname + '/scripts'));

// Technology not needed but good practice, especailly when serval people are working on it 
app.get('/', function (req, res) {
    res.sendFile('index.html');
}); 

// listen for contactlist get request, aka transfers the contacklist in mongo to client
app.get('/databases', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {

      // Use the admin database for the operation
      var adminDb = db.admin();

      // List all the available databases
      adminDb.listDatabases(function(err, dbs) {
        assert.equal(null, err);
        assert.ok(dbs.databases.length > 0);
        console.log(dbs);
        res.json(dbs); 
        db.close();
      });
    });
}); 

// listen for contactlist get request
app.post('/collection', function (req, res) {
    console.log("-- recived collection post request --"); 
    console.log('req ' + req.body);
    res.json(req.body);

    db.open(function(err, db) {
         // Grab a collection without a callback no safe mode
         // once request is working will switch to re.body
        var col1 = db.collection('DB');
    });
}); 



// Implement a web server to listen to requests 
app.listen(4444, function(){
    console.log('ready on port 4444'); 
}); 

enter image description here

修复

enter image description here

最佳答案

响应是一个对象,试试这个:

DB name clicked: <p ng-bind="DB_NAME | json"></p>

json 过滤器将对象转换为 JSON 表示法:https://docs.angularjs.org/api/ng/filter/json

关于javascript - Post 请求未通过 Angular.js 发送。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417913/

相关文章:

javascript - 导入处理JS?

javascript - 如何将Powerbi报表嵌入 flutter 的android应用程序中?

javascript - Imbrication promise 上传文件 angularJS

javascript - 用 Jasmine 测试异步功能

javascript - React/Electron 渲染组件失败

javascript - for 循环内的 Gulp 流无法正常工作

node.js - 在heroku中 Sequelize 迁移

javascript - 在 AngularJS 指令上需要 :ngModel vs. 范围:{ ngModel: '=' }

javascript - AngularJS 的 REST 服务缓存策略

javascript - 使用 Javascript 中的一维数组对锯齿状多维数组进行逐元素过滤