Code is not working when i am trying to edit the API URL by data entered in form and fetch data from API
up vote
0
down vote
favorite
Hello everyone as I am beginner in nodejs and expressjs, I am trying to fetch data according to the value entered in the form. And according to the value URL will be edited and send request and get data and show it to page. I wrote the code but it is not working.
Index.ejs
<form action="/index" method="POST">
<label for="name">Enter json/xml: </label>
<input type="text" name="name">
     <button type="submit">Submit</button>
</form>
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
node.js api express ejs restful-url
add a comment |
up vote
0
down vote
favorite
Hello everyone as I am beginner in nodejs and expressjs, I am trying to fetch data according to the value entered in the form. And according to the value URL will be edited and send request and get data and show it to page. I wrote the code but it is not working.
Index.ejs
<form action="/index" method="POST">
<label for="name">Enter json/xml: </label>
<input type="text" name="name">
     <button type="submit">Submit</button>
</form>
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
node.js api express ejs restful-url
Please add more information! Add more info about the issue you're facing. Its not working wouldn't help anybody out :)
– vibhor1997a
Nov 20 at 13:03
It´s really hard to help you with the info provided but maybe you seem to be posting to "/index" and having express serve on "/" ?
– Antonio Ortells
Nov 20 at 15:43
@AntonioOrtells sir as i am on learning stage, i am not sure of these things. I am just finding the exact way to run code in such type of conditions, I just want to enter the value in the form then according to the form value it will call api URL and fetch data and show on other template file
– Abhishek Rawal
Nov 21 at 6:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hello everyone as I am beginner in nodejs and expressjs, I am trying to fetch data according to the value entered in the form. And according to the value URL will be edited and send request and get data and show it to page. I wrote the code but it is not working.
Index.ejs
<form action="/index" method="POST">
<label for="name">Enter json/xml: </label>
<input type="text" name="name">
     <button type="submit">Submit</button>
</form>
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
node.js api express ejs restful-url
Hello everyone as I am beginner in nodejs and expressjs, I am trying to fetch data according to the value entered in the form. And according to the value URL will be edited and send request and get data and show it to page. I wrote the code but it is not working.
Index.ejs
<form action="/index" method="POST">
<label for="name">Enter json/xml: </label>
<input type="text" name="name">
     <button type="submit">Submit</button>
</form>
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
node.js api express ejs restful-url
node.js api express ejs restful-url
asked Nov 20 at 12:59
Abhishek Rawal
708
708
Please add more information! Add more info about the issue you're facing. Its not working wouldn't help anybody out :)
– vibhor1997a
Nov 20 at 13:03
It´s really hard to help you with the info provided but maybe you seem to be posting to "/index" and having express serve on "/" ?
– Antonio Ortells
Nov 20 at 15:43
@AntonioOrtells sir as i am on learning stage, i am not sure of these things. I am just finding the exact way to run code in such type of conditions, I just want to enter the value in the form then according to the form value it will call api URL and fetch data and show on other template file
– Abhishek Rawal
Nov 21 at 6:41
add a comment |
Please add more information! Add more info about the issue you're facing. Its not working wouldn't help anybody out :)
– vibhor1997a
Nov 20 at 13:03
It´s really hard to help you with the info provided but maybe you seem to be posting to "/index" and having express serve on "/" ?
– Antonio Ortells
Nov 20 at 15:43
@AntonioOrtells sir as i am on learning stage, i am not sure of these things. I am just finding the exact way to run code in such type of conditions, I just want to enter the value in the form then according to the form value it will call api URL and fetch data and show on other template file
– Abhishek Rawal
Nov 21 at 6:41
Please add more information! Add more info about the issue you're facing. Its not working wouldn't help anybody out :)
– vibhor1997a
Nov 20 at 13:03
Please add more information! Add more info about the issue you're facing. Its not working wouldn't help anybody out :)
– vibhor1997a
Nov 20 at 13:03
It´s really hard to help you with the info provided but maybe you seem to be posting to "/index" and having express serve on "/" ?
– Antonio Ortells
Nov 20 at 15:43
It´s really hard to help you with the info provided but maybe you seem to be posting to "/index" and having express serve on "/" ?
– Antonio Ortells
Nov 20 at 15:43
@AntonioOrtells sir as i am on learning stage, i am not sure of these things. I am just finding the exact way to run code in such type of conditions, I just want to enter the value in the form then according to the form value it will call api URL and fetch data and show on other template file
– Abhishek Rawal
Nov 21 at 6:41
@AntonioOrtells sir as i am on learning stage, i am not sure of these things. I am just finding the exact way to run code in such type of conditions, I just want to enter the value in the form then according to the form value it will call api URL and fetch data and show on other template file
– Abhishek Rawal
Nov 21 at 6:41
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I think you have a problem with 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');
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
add a comment |
up vote
0
down vote
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);
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393557%2fcode-is-not-working-when-i-am-trying-to-edit-the-api-url-by-data-entered-in-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think you have a problem with 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');
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
add a comment |
up vote
1
down vote
accepted
I think you have a problem with 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');
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
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think you have a problem with 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');
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
I think you have a problem with 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');
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
answered Nov 23 at 7:27
Nuriddin Kudratov
656
656
add a comment |
add a comment |
up vote
0
down vote
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);
});
add a comment |
up vote
0
down vote
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);
});
add a comment |
up vote
0
down vote
up vote
0
down vote
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);
});
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);
});
answered Nov 20 at 17:49
Raunik Singh
844
844
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393557%2fcode-is-not-working-when-i-am-trying-to-edit-the-api-url-by-data-entered-in-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please add more information! Add more info about the issue you're facing. Its not working wouldn't help anybody out :)
– vibhor1997a
Nov 20 at 13:03
It´s really hard to help you with the info provided but maybe you seem to be posting to "/index" and having express serve on "/" ?
– Antonio Ortells
Nov 20 at 15:43
@AntonioOrtells sir as i am on learning stage, i am not sure of these things. I am just finding the exact way to run code in such type of conditions, I just want to enter the value in the form then according to the form value it will call api URL and fetch data and show on other template file
– Abhishek Rawal
Nov 21 at 6:41