【问题标题】:Receive JSON from Axios request in NodeJS在 NodeJS 中从 Axios 请求接收 JSON
【发布时间】:2019-08-26 13:49:36
【问题描述】:

我正在尝试在 NodeJS 中接收我通过 axios 发布请求发送的 JSON,但我不知道如何接收该 JSON 以及如何读取它。 Axios 发布请求由一个 url 发出,该 url 从带有表单操作的 html 页面接收数据,然后 axios 向应该接收该 JSON 并在页面上打印的第二个 url 发出 post 请求。我该怎么办?

谢谢

更新:现在我不知道为什么 Axios 没有找到它必须发布的 url,它响应错误 404

HTML:

<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>

Actions :
<input type = "text" list = "actions" name = "action">
  <datalist id = "actions">
  <option value="Create"></option>
  <option value="Update"></option>
  <option value="Delete"></option>
</datalist>

<br>
<br>

Type :
<input type = "text" list = "types" name = "type">
  <datalist id = "types">
  <option value="Person"></option>
  <option value="Companies"></option>
  <option value="Opportunities"></option>
  <option value="Lead"></option>
  <option value="Projects"></option>
  <option value="Tasks"></option>
  <option value="Activities"></option>
</datalist>


<br>
<br>

ID: <input type="text" id="demo" name="id">
    <br>
    <br>
ID1: <input type="text" id="demo1" name="id1">       
    <br>
    <br>
Subscription ID: <input type="text"  id="demo2" name="subid">
    <br>
    <br>
Old Name: <input type="text"  id="demo3" name="oname">
    <br>
    <br>
New Name: <input type="text"  id="demo4" name="nname">
    <br>
    <br>
<input type="submit"  value="Submit"> 

</form>

使 Axios POST 的 NodeJS

exports.notification = (req, res) => {

var express = require('express');

var axios = require('axios');

var https = require('https');

var bodyParser = require('body-parser');

var app = express();



var config = {

 path : '/',

 headers: {'Content-Type' : 'application/x-www-form-urlencoded',

 'Content-Type' : 'text/html',

 'Content-Type' : 'application/json' }

};



var info = {

 ids:[req.body.id,req.body.id1],

 type: req.body.type,

 event: req.body.action,

 subscription_id: req.body.subid,

 secret_field_1:null,

 secret_field_2:null,

 updated_attributes:{field_name:[req.body.oname,req.body.nname]}

}  



var myJSON = JSON.stringify(info);



return axios.post('/notification-example', info, config)

 .then((result) => {

   console.log("DONE",result);

 })

 .catch((err) => {

   console.log("ERROR",err);

 })
};

接收并打印 JSON 的第二个 url 的 NodeJS

const express = require ('express');
const https = require ('https');
const bodyParser = require ('body-parser');

const app = express();

const port  = 8080;

// ROUTES
var router = express.Router(); // get an instance of router
router.use(function(req, res, next) { // route middleware that will happen on every request
 console.log(req.method, req.url); // log each request to the console
 next(); // continue doing what we were doing and go to the route
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/notification-example',require ('./Routers/API/notification_example'));

app.use('/', router); // apply the routes to our application

// START THE SERVER
// ==============================================
app.listen(port);
console.log('Listening ' + port);

module.exports={
  app
};

通知文件

const express = require('express');
const router = express.Router();

//const notification_example = require('../../Notification');

router.get('/', function(req, res) {
    ????
});

module.exports = router;

【问题讨论】:

  • 您为什么认为这起作用?
  • 我不知道。这是我第一次接触这一切,如果你有什么建议,我会很高兴听到的。任何帮助表示赞赏,谢谢

标签: node.js json express axios


【解决方案1】:

这里发生了很多事情,但如果您将???? 替换为res.bodyres.data,您应该会得到您正在寻找的JSON 数据。

【讨论】:

  • 我不知道怎么做,但现在第一个 url 用错误代码 404 回复我。这怎么可能?
  • 您没有POST 路由。我只在Notifican file 示例中看到router.get
猜你喜欢
  • 1970-01-01
  • 2020-08-20
  • 2019-01-06
  • 2021-09-01
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多