节点js和角度js

时间:2017-01-06 10:16:42

标签: javascript angularjs node.js angularjs-1.6

我要通过Angular js向用户发送用户输入的文本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)

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

5 个答案:

答案 0 :(得分:0)

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

启动了服务

答案 1 :(得分: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

更新

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

  

由于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 Developer Guide - Migrating to v1.6 - http

答案 2 :(得分: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();

答案 3 :(得分:0)

在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 = {};
})

答案 4 :(得分:0)

您正在将数据传递到请求的主体中(以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);
});
相关问题