角后JSON来恩preSSJSON、preSS

2023-09-13 04:50:50 作者:废物·

我想一个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