我正在尝试接收随axios发布请求发送的NodeJS中的JSON,但我不知道如何接收该JSON以及如何读取它。 Axios发布请求是由通过表单操作从html页面接收数据的URL发出的,然后axios向第二个应接收该JSON并打印在页面上的URL发出发布请求。我该怎么办?
谢谢
更新:现在,我不知道为什么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
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;
答案 0 :(得分:0)
这里有很多事情要做,但是如果您将????
替换为res.body
或res.data
,则应该获取所需的JSON
数据。