从NodeJS中的Axios请求接收JSON [英] Receive JSON from Axios request in NodeJS

查看:62
本文介绍了从NodeJS中的Axios请求接收JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to receive a JSON in NodeJS that I sent with an axios post request, but I don't know how receive that JSON and how read it. Axios post request is made by an url that receive data from html page with form action, then axios make a post request to the second url that should receive that JSON and print on the page. How can I do?

谢谢

更新:现在,我不知道为什么Axios找不到要发布的网址,它的响应为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

NodeJS that make Axios POST

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

NodeJS of second url that receive and print the JSON

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;

推荐答案

这里发生了很多事情,但是如果您将 ???? 替换为 res.body res.data ,则应获取要查找的 JSON 数据.

Theres a lot going on here but if you replace the ???? with res.body or res.data, you should get the JSON data you are looking for.

这篇关于从NodeJS中的Axios请求接收JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆