角后JSON来恩preSS [英] angular post json to express

查看:119
本文介绍了角后JSON来恩preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个JSON发送到服务器节点/ EX preSS与 angular.js

I'm trying to send a json to a server node/express with angular.js

我server.js

my server.js

/*
* Setup
*/
// Dependencies
var express = require('express');
// Start Express
var app = express();
// Conf port
var port = process.env.PORT || 3000;
/*
* Conf. the app
*/
    app.configure(function () {
        app.use(express.static(__dirname + '/public'));
        app.use(express.logger('dev'));
        app.use(express.bodyParser());
        app.use(express.methodOverride());
    });

/*
* Routes
*/
require('./app/routes')(app);

/*
* Listen
*/
app.listen(port);
console.log("App listening on port " + port);

我Routes.js

My Routes.js

module.exports = function (app) {

app.post('/post/:name', function (req, res) {
    console.log("Serv get [OK]");
    /*
    * Disparar broadcast para all
    */
});
app.get('/', function (req, res) {
    res.sendfile('./public/view/index.html');
});
}

在我的服务器正在接收一个帖子,我使用:

When my server is receiving a POST, I use:

app.post('/post'...
   OR
app.get('/get'...

捕捉路线?

我的角度应用程序是好吗?

My angular app is ok?

webchatApp = angular.module("webchatApp",[]);

webchatApp.controller('webchatCtrl', function ($scope,$http) {
$scope.sendMsg = function () {
    var dados = {name:"test message"};
    $http.post({url:'http://localhost/post/',data:dados})
        .success(function (data) {
            console.log("Success" + data);
        })
        .error(function(data) {
            console.log("Erro: "+data);
        })
};
  });

错误:无法发布/ [对象%20Object]
有什么毛病我的帖子,结果将发送: [对象%20Object]

推荐答案

尝试改变这一点:

$http({
    method: 'POST',
    url: 'http://localhost/post/',
    data: dados
})
.success(function() {})
.error(function() {});

看起来你使用不正确 $ http.post 方法签名。它应该是:

$http.post('http://localhost/post/', dados)
.success(...)
.error(...);

...既然成功错误方法去precated,它最好是

... and since success and error methods are deprecated, it should better be

$http.post('http://localhost/post/', dados)
.then(function() {...})   // success
.catch(function() {...}); // error

这篇关于角后JSON来恩preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆