[英]unable to post to node.js - express from angularjs
我正在嘗試將一些數據發布到node.js-我的angularjs前端的Express服務器,並且我認為我的發布請求從未到達我的服務器。
這是前端的一些基本angularjs:
myApp.controller('myController', ['$scope', '$http', function($scope, $http){
$scope.items = null;
$http({method: 'POST',
url: 'http://localhost:5000/post',
data: 'some data'
}).success(function(data){
$scope.items = data;
console.log(data);
}).error(function(data, statusText){
console.log(statusText + ': ' + data);
});
}]);
這是一個基本的后端:
var express = require('express');
var app = express();
app.post('/post', function(req, res){
console.log(req.url);
console.log(req.method);
console.log(req.body);
res.end('some response data');
});
app.listen(5000);
但是,服務器未記錄從前端發送的任何數據。 我不確定POST請求是否甚至到達服務器,因為它沒有將任何數據記錄到控制台。 任何幫助/建議,將不勝感激。
檢查標題。
data = 'some data';
$http({
host: "localhost:5000",
port: "80",
path: "post",
method: "POST",
headers: {
//"User-Agent": "NodeJS/1.0",
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": data.length
},
data: data
});
也許添加$http.write(data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.