【问题标题】:angular post json to express角度帖子json来表达
【发布时间】:2014-01-07 16:17:22
【问题描述】:

我正在尝试使用 angular.js 将 json 发送到服务器节点/快递

我的 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

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');
});
}

当我的服务器收到 POST 时,我使用:

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

捕捉路线?

我的 Angular 应用还好吗?

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);
        })
};
  });

错误:Cannot POST /[object%20Object] 我的帖子有问题,
它会发送:[object% 20Object]

【问题讨论】:

    标签: angularjs post types express


    【解决方案1】:

    试着改成这样:

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

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

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

    ...由于successerror 方法已被弃用,最好是

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2023-03-25
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多