Send array from node.js and use client side

Multi tool use
up vote
2
down vote
favorite
Im a beginner with node.js so bear with me please :D
Simple task: I use express
and I want to send an array, lets say ["item1", "item2"]
from a node server to a client that calls the server with a get
method. When I tried this, I stumbled upon the CORS
error.
I also thought about doing a this through a post
:
-client
:
$(document).ready(function () {
$(".testButton").click(function (e)
e.preventDefault();
$.ajax(
url: "http://localhost:3000/test_post",
type: "post",
data: "sent",
success: function ()
);
);
-server
:
app.post('/Quiz_post', function (req, res)
res.send(["item1", "item2"]);
);
But this also doesnt work. Now I am trying to use cross-fetch
client side. Could you please guide me a bit? Thanks!
node.js express post get
add a comment |
up vote
2
down vote
favorite
Im a beginner with node.js so bear with me please :D
Simple task: I use express
and I want to send an array, lets say ["item1", "item2"]
from a node server to a client that calls the server with a get
method. When I tried this, I stumbled upon the CORS
error.
I also thought about doing a this through a post
:
-client
:
$(document).ready(function () {
$(".testButton").click(function (e)
e.preventDefault();
$.ajax(
url: "http://localhost:3000/test_post",
type: "post",
data: "sent",
success: function ()
);
);
-server
:
app.post('/Quiz_post', function (req, res)
res.send(["item1", "item2"]);
);
But this also doesnt work. Now I am trying to use cross-fetch
client side. Could you please guide me a bit? Thanks!
node.js express post get
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Im a beginner with node.js so bear with me please :D
Simple task: I use express
and I want to send an array, lets say ["item1", "item2"]
from a node server to a client that calls the server with a get
method. When I tried this, I stumbled upon the CORS
error.
I also thought about doing a this through a post
:
-client
:
$(document).ready(function () {
$(".testButton").click(function (e)
e.preventDefault();
$.ajax(
url: "http://localhost:3000/test_post",
type: "post",
data: "sent",
success: function ()
);
);
-server
:
app.post('/Quiz_post', function (req, res)
res.send(["item1", "item2"]);
);
But this also doesnt work. Now I am trying to use cross-fetch
client side. Could you please guide me a bit? Thanks!
node.js express post get
Im a beginner with node.js so bear with me please :D
Simple task: I use express
and I want to send an array, lets say ["item1", "item2"]
from a node server to a client that calls the server with a get
method. When I tried this, I stumbled upon the CORS
error.
I also thought about doing a this through a post
:
-client
:
$(document).ready(function () {
$(".testButton").click(function (e)
e.preventDefault();
$.ajax(
url: "http://localhost:3000/test_post",
type: "post",
data: "sent",
success: function ()
);
);
-server
:
app.post('/Quiz_post', function (req, res)
res.send(["item1", "item2"]);
);
But this also doesnt work. Now I am trying to use cross-fetch
client side. Could you please guide me a bit? Thanks!
node.js express post get
node.js express post get
asked Nov 10 at 19:23


theLotus
285
285
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Add this code in ur app.js to enable CORS.
npm i cors in the project 1st
var cors = require('cors');
var app= express(); //After this line of code add the code below
app.use(cors());
2
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
2
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Add this code in ur app.js to enable CORS.
npm i cors in the project 1st
var cors = require('cors');
var app= express(); //After this line of code add the code below
app.use(cors());
2
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
2
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
add a comment |
up vote
0
down vote
accepted
Add this code in ur app.js to enable CORS.
npm i cors in the project 1st
var cors = require('cors');
var app= express(); //After this line of code add the code below
app.use(cors());
2
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
2
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Add this code in ur app.js to enable CORS.
npm i cors in the project 1st
var cors = require('cors');
var app= express(); //After this line of code add the code below
app.use(cors());
Add this code in ur app.js to enable CORS.
npm i cors in the project 1st
var cors = require('cors');
var app= express(); //After this line of code add the code below
app.use(cors());
answered Nov 10 at 20:05


Marcia Ong
475311
475311
2
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
2
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
add a comment |
2
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
2
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
2
2
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
Thanks. This removed the CORS err. Now I am figuring out the next part: sending the actual array/json form the server and using it on the client.
– theLotus
Nov 12 at 9:30
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
To send JSON data back just res.json(Object/array). You can test your server api by using postman
– Marcia Ong
Nov 12 at 12:38
2
2
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
Already did it, but thank you the comment. Thanks a lot for the answer! You helped me a lot :)
– theLotus
Nov 12 at 13:41
add a comment |
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%2f53242604%2fsend-array-from-node-js-and-use-client-side%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
OBt nrJe,F1U,R0