我要通过Angular js发送用户输入的文本到Node js,问题是节点js服务器识别该事件,但没有获得数据并打印它,我知道我在代码中做了一些错误,不知道如何修复它。有人能帮我修一下这个代码吗?
angular.js
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.submit= function(){
var data = {
id : "Angkirat"
Password : "Sandhu"
}
$http.post("http://localhost:8081/meow", data)
.success(function(req,res) {
console.dir(req.body);
res.send(req.body);
alert("hogaya")
}).error(function() {
});
}
});Server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cors = require('cors');
var http = require("http");
var fs = require("fs");
var url = require("url");
var display = "";
http.createServer(function(request,response){
var pathname = url.parse(request.url).pathname;
console.log("Request for" + pathname + "received.");
fs.readFile(pathname.substr(1),function (err,data){
if(err){
console.log(err);
response.writeHead(404, {'Content-Type': 'text/html'});
}else{
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data.toString());
}
app.use(cors());
app.use(bodyParser.json());
app.post('/meow', function(req, res){
var cope = req.body.params;
display = cope;
console.log(cope);
});
response.end();
});
}).listen(8080);
console.log(display)请找个人帮我解决这个问题。
发布于 2017-01-06 18:20:37
您正在8080中启动服务器,但正在从http://localhost:8081/meow中访问您的服务
发布于 2017-01-06 22:20:35
服务器代码在客户端代码中
//ERRONEOUS
$http.post("http://localhost:8081/meow", data)
.success(
//-------------------------------------
//THESE LINES BELONG IN THE SERVER CODE
function(req,res) {
console.dir(req.body);
res.send(req.body);
//-------------------------------------
alert("hogaya")
}).error(function() {
});$http服务的.success方法具有不同的签名:
$http.post(url, data)
.success(function onSuccess(data, status, headers, config) {
// Handle success更新
不推荐使用的.success和.error方法已从AngularJS 1.6中删除。
由于b54a39,
$http不推荐使用的自定义回调方法.success()和.error()已被移除。您可以改用标准的.then()/.catch()promise方法,但请注意,方法签名和返回值是不同的。
$http(...)
.then(function onSuccess(response) {
// Handle success
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
...
}).catch(function onError(response) {
// Handle error
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
...
});有关更多信息,请参阅AngularJS Developer Guide - Migrating to v1.6 - http
发布于 2017-07-05 04:40:34
在server.js文件控制台中使用req.body而不是req.body.params
app.post('/meow', function(req, res){
console.log(req.body);
console.log(req.body.id) //to access the id
console.log(req.body.Password) //to access the password
});
response.end();https://stackoverflow.com/questions/41503462
复制相似问题