首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node js和Angular js

Node js和Angular js
EN

Stack Overflow用户
提问于 2017-01-06 18:16:42
回答 5查看 493关注 0票数 1

我要通过Angular js发送用户输入的文本到Node js,问题是节点js服务器识别该事件,但没有获得数据并打印它,我知道我在代码中做了一些错误,不知道如何修复它。有人能帮我修一下这个代码吗?

angular.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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)

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

EN

回答 5

Stack Overflow用户

发布于 2017-01-06 18:20:37

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

票数 0
EN

Stack Overflow用户

发布于 2017-01-06 22:20:35

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

代码语言:javascript
复制
  //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方法具有不同的签名:

代码语言:javascript
复制
 $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方法,但请注意,方法签名和返回值是不同的。

代码语言:javascript
复制
$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

票数 0
EN

Stack Overflow用户

发布于 2017-07-05 04:40:34

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

代码语言:javascript
复制
    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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41503462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档