简体   繁体   English

当我尝试通过表单中输入的数据来编辑API URL并从API提取数据时,代码不起作用

[英]Code is not working when i am trying to edit the API URL by data entered in form and fetch data from API

Hello everyone as I am beginner in nodejs and expressjs, I am trying to fetch data according to the value entered in the form. 大家好,我是nodejs和expressjs的初学者,我正在尝试根据在表单中输入的值来获取数据。 And according to the value URL will be edited and send request and get data and show it to page. 然后根据该值将编辑URL,并发送请求并获取数据并将其显示到页面。 I wrote the code but it is not working. 我写了代码,但是没有用。

Index.ejs Index.ejs

    <form action="/index" method="POST">
        <label for="name">Enter json/xml: </label>
        <input type="text" name="name">
        &nbsp&nbsp&nbsp&nbsp&nbsp<button type="submit">Submit</button>
    </form>

app.js app.js

    var express = require('express');
    var app = express();
    var request = require('request');
    var bodyParser = require('body-parser');

    app.set('view engine', 'ejs');

    var urlencoderparser = bodyParser.urlencoded({ extended : true });

    app.get('/', function(req, res){
        res.render('index');
    });

    app.post('/', urlencoderparser, function(req, res){
        res.render('form-data', {data : req.body});
        request.post({
        "headers": { "content-type": "application/json" },
        "url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+req.body.name,
        }, (error, response, body) => {
            if(error) {
                return console.log(error);
            }
            data = JSON.parse(body);
        });
        console.log(req.body);
    });

Please help me to find the solution 请帮我找到解决方案

I think you have a problem with bodyParser. 我认为您的bodyParser有问题。

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

Add these just after app.set('view engine', 'ejs'); app.set('view engine', 'ejs');之后添加它们

And your post function should be like this 你的帖子功能应该是这样的

     app.post('/', function(req, res){
     request('http://mysafeinfo.com/api/data?list=englishmonarchs&format='+req.body.name, function (error, response, body) {
     var data = JSON.parse(body);
     console.log(req.body);
    });
    });

And make sure that in the form in your index file you should have <input type="text" name="name"> Because you are using as req.body.name 并确保在索引文件的表单中,您应该具有<input type="text" name="name">因为您使用的是req.body.name

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

app.post('/index', function(req, res){
        res.render('form-data', {data : req.body});
        request.post({
        "headers": { "content-type": "application/json" },
        "url": "http://mysafeinfo.com/api/data?list=englishmonarchs&format="+req.body.name,
        }, (error, response, body) => {
            if(error) {
                return console.log(error);
            }
            data = JSON.parse(body);
        });
        console.log(req.body);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM