繁体   English   中英

节点js和角度js

[英]Node js and Angular js

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

angular.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() {

      });
   }
});

Server.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)

请允许一些人为我解决这个问题。

您正在8080中启动服务器,但是您从http://localhost:8081/meow

服务器代码位于客户端代码中

  //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

UPDATE

已弃用的.success.error方法已从AngularJS 1.6中删除。

由于b54a39$http已弃用的自定义回调方法 - .success().error() - 已被删除。 您可以使用标准的.catch() .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

在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();

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

.config(function($ 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 = {};
})

您正在传递请求正文中的数据(在angularJS中)但尝试从params(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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM