Send array from node.js and use client side









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!










share|improve this question

























    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!










    share|improve this question























      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!










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 19:23









      theLotus

      285




      285






















          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());





          share|improve this answer
















          • 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










          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
          );



          );













           

          draft saved


          draft discarded


















          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

























          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());





          share|improve this answer
















          • 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














          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());





          share|improve this answer
















          • 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












          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());





          share|improve this answer












          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());






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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












          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党