Mat*_*ahl 4 post types express angularjs
我正在尝试将json发送到服务器节点/ express angular.js
我的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);
Run Code Online (Sandbox Code Playgroud)
我的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');
});
}
Run Code Online (Sandbox Code Playgroud)
当我的服务器收到POST时,我使用:
app.post('/post'...
OR
app.get('/get'...
Run Code Online (Sandbox Code Playgroud)
捕捉路线?
我的角度应用可以吗?
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);
})
};
});
Run Code Online (Sandbox Code Playgroud)
错误:Cannot POST /[object%20Object]
我的帖子有问题,
它会发送:[object%20Object]
尝试改为:
$http({
method: 'POST',
url: 'http://localhost/post/',
data: dados
})
.success(function() {})
.error(function() {});
Run Code Online (Sandbox Code Playgroud)
看起来你使用了错误的$http.post方法签名.它应该是:
$http.post('http://localhost/post/', dados)
.success(...)
.error(...);
Run Code Online (Sandbox Code Playgroud)
...并且由于success和error方法被弃用,最好是
$http.post('http://localhost/post/', dados)
.then(function() {...}) // success
.catch(function() {...}); // error
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10563 次 |
| 最近记录: |