本文介绍了快速访问Angular Http Post数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法访问从角度http服务传递到express.js的数据.

I have been having trouble accessing the data that is passed to express.js from angular http service.

我正在使用http服务:

I am using the http service :

$http.post('/someUrl', data, config).then(successCallback, errorCallback);

这就是我正在做的事情:

This is what I am doing in angular:

$http.post('/api/topic', {data : topic}).then(successCallback, errorCallback);

这就是我正在做的快递:

This is what I am doing on in express:

app.post('/api/topic', function(req,res){console.log(req.data);});

我们假设主题是从angular的前端接收到的对象.

We are assuming the topic is an object recieved from angular's frontend.

{
    username : 'dan',
    topic : 'I want to learn more',
    description : 'Where can I learn more about web development',
    category : 'web development'
}

所以我的问题是:使用express的后端如何访问有角度的http数据字段?

So my question is : How does the backend end using express access the angular http data field?

推荐答案

对于POST请求,您需要正文解析器(获取请求仍可访问数据)

for POST requests you need body parser (get requests still access data)

var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer'); // v1.0.5

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.post('/api/topic', function (req, res, next) {
  console.log(req.body);
  res.json(req.body);
});

这篇关于快速访问Angular Http Post数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:44