1

我要通过Angular js将用户输入的文本发送到Node js问题是节点js服务器识别事件但没有获取数据并打印它我知道我在代码中犯了一些错误并且不知道如何修复它。有人可以请修复此代码。

角.js

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.submit= function(){
      var data = {
          id : "Angkirat"
          Password : "Sandhu"
      }

      $http.post("http://localhost:8081/meow", data)
        .success(function(req,res) {
         console.dir(req.body);
         res.send(req.body);
         alert("hogaya")
       }).error(function() {

      });
   }
});

服务器.js

var express = require('express'); 
var app = express();
var bodyParser = require('body-parser');
var cors = require('cors');
var http = require("http");
var fs = require("fs");
var url = require("url");
var display = "";

http.createServer(function(request,response){
    var pathname = url.parse(request.url).pathname;
    console.log("Request for" + pathname + "received.");
    fs.readFile(pathname.substr(1),function (err,data){
        if(err){
            console.log(err);
            response.writeHead(404, {'Content-Type': 'text/html'});
        }else{
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write(data.toString());
        }
        app.use(cors());
        app.use(bodyParser.json());
        app.post('/meow', function(req, res){
            var cope = req.body.params;
            display = cope;
            console.log(cope);
        });
        response.end();
    });
}).listen(8080);
console.log(display)

请有人帮我解决这个问题。

4

5 回答 5

0

在 server.js 文件控制台中 req.body 而不是 req.body.params

    app.post('/meow', function(req, res){
    console.log(req.body);
    console.log(req.body.id) //to access the id
    console.log(req.body.Password) //to access the password
    });
    response.end();
于 2017-07-04T20:40:34.253 回答
0

您正在启动服务器8080,但您的服务来自http://localhost:8081/meow

于 2017-01-06T10:20:37.283 回答
0

在您的 Angular 中设置以下标题,它应该可以工作

.config(函数($httpProvider){

    $httpProvider.defaults.headers.common = {};
    $httpProvider.defaults.headers.post = { 'Content-Type': 'application/json' };
    $httpProvider.defaults.headers.put = { 'Content-Type': 'application/json' };
    $httpProvider.defaults.headers.patch = {};
})
于 2018-02-05T06:42:21.953 回答
0

您正在请求正文中传递数据(在 angularJS 中),但尝试从参数(Node.JS)访问数据

app.post('/meow', function(req, res){
  var cope = req.body.params;   //Change this line to var cope = req.body
  display = cope;
  console.log(cope);
});
于 2018-08-06T09:28:55.830 回答
0

服务器代码在客户端代码中

  //ERRONEOUS
  $http.post("http://localhost:8081/meow", data)
    .success(
      //-------------------------------------
      //THESE LINES BELONG IN THE SERVER CODE
      function(req,res) {
        console.dir(req.body);
        res.send(req.body);
      //-------------------------------------
        alert("hogaya")
   }).error(function() {

  });

$http 服务的.success方法有不同的签名:

 $http.post(url, data)
   .success(function onSuccess(data, status, headers, config) {
       // Handle success

更新

AngularJS 1.6 中删除了不推荐使用的.success和方法。.error

由于b54a39$http已弃用的自定义回调方法 -.success()并且.error()- 已被删除。您可以改用标准.then()/ .catch()promise 方法,但请注意方法签名和返回值是不同的。

$http(...)
  .then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  }).catch(function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  });

有关更多信息,请参阅AngularJS 开发人员指南 - 迁移到 v1.6 - http

于 2017-01-06T14:20:35.167 回答