Spring + React fetch data












1















i have a problem with fetch data from api exposed by spring.
I want to fetch data by:



componentDidMount() {
this.setState({isLoading: true});

fetch('http://localhost:8080/api/tasks/')
.then(response => { return response.json() })
.then(results => this.setState({
tasks: results,
isLoading: false
}));


The spring api which return data:



    @CrossOrigin(origins = "http://localhost:3000")
@GetMapping
public List<Task> findTasksByUserId() {
return taskService.findTasksBelongToUser(idProvider.getCurrentUserId());
}


Returned JSON:



[
{
id: 1,
version: 0,
name: "Task1",
description: "description1",
priority: 1,
finished: true,
category: {
id: 1,
version: 0,
name: "Uncategorized",
user: {
id: 1,
version: 0,
login: "admin",
email: "admin@todo.pl",
enabled: true
}
},
user: {
id: 1,
version: 0,
login: "admin",
email: "admin@todo.pl",
enabled: true
},
position: 0
},
{
id: 2,
version: 0,
name: "Task2",
description: "description2",
priority: 4,
finished: true,
category: {
id: 1,
version: 0,
name: "Uncategorized",
user: {
id: 1,
version: 0,
login: "admin",
email: "admin@todo.pl",
enabled: true
}
},
user: {
id: 1,
version: 0,
login: "admin",
email: "admin@todo.pl",
enabled: true
},
position: 1
}]


And i got a error like this:



Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0


I dont know what is wrong but maybe format of json is not correct, because it's start with the sign '['.
I am a beginner in React so please about some hint and help.
regards










share|improve this question



























    1















    i have a problem with fetch data from api exposed by spring.
    I want to fetch data by:



    componentDidMount() {
    this.setState({isLoading: true});

    fetch('http://localhost:8080/api/tasks/')
    .then(response => { return response.json() })
    .then(results => this.setState({
    tasks: results,
    isLoading: false
    }));


    The spring api which return data:



        @CrossOrigin(origins = "http://localhost:3000")
    @GetMapping
    public List<Task> findTasksByUserId() {
    return taskService.findTasksBelongToUser(idProvider.getCurrentUserId());
    }


    Returned JSON:



    [
    {
    id: 1,
    version: 0,
    name: "Task1",
    description: "description1",
    priority: 1,
    finished: true,
    category: {
    id: 1,
    version: 0,
    name: "Uncategorized",
    user: {
    id: 1,
    version: 0,
    login: "admin",
    email: "admin@todo.pl",
    enabled: true
    }
    },
    user: {
    id: 1,
    version: 0,
    login: "admin",
    email: "admin@todo.pl",
    enabled: true
    },
    position: 0
    },
    {
    id: 2,
    version: 0,
    name: "Task2",
    description: "description2",
    priority: 4,
    finished: true,
    category: {
    id: 1,
    version: 0,
    name: "Uncategorized",
    user: {
    id: 1,
    version: 0,
    login: "admin",
    email: "admin@todo.pl",
    enabled: true
    }
    },
    user: {
    id: 1,
    version: 0,
    login: "admin",
    email: "admin@todo.pl",
    enabled: true
    },
    position: 1
    }]


    And i got a error like this:



    Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0


    I dont know what is wrong but maybe format of json is not correct, because it's start with the sign '['.
    I am a beginner in React so please about some hint and help.
    regards










    share|improve this question

























      1












      1








      1








      i have a problem with fetch data from api exposed by spring.
      I want to fetch data by:



      componentDidMount() {
      this.setState({isLoading: true});

      fetch('http://localhost:8080/api/tasks/')
      .then(response => { return response.json() })
      .then(results => this.setState({
      tasks: results,
      isLoading: false
      }));


      The spring api which return data:



          @CrossOrigin(origins = "http://localhost:3000")
      @GetMapping
      public List<Task> findTasksByUserId() {
      return taskService.findTasksBelongToUser(idProvider.getCurrentUserId());
      }


      Returned JSON:



      [
      {
      id: 1,
      version: 0,
      name: "Task1",
      description: "description1",
      priority: 1,
      finished: true,
      category: {
      id: 1,
      version: 0,
      name: "Uncategorized",
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      }
      },
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      },
      position: 0
      },
      {
      id: 2,
      version: 0,
      name: "Task2",
      description: "description2",
      priority: 4,
      finished: true,
      category: {
      id: 1,
      version: 0,
      name: "Uncategorized",
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      }
      },
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      },
      position: 1
      }]


      And i got a error like this:



      Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0


      I dont know what is wrong but maybe format of json is not correct, because it's start with the sign '['.
      I am a beginner in React so please about some hint and help.
      regards










      share|improve this question














      i have a problem with fetch data from api exposed by spring.
      I want to fetch data by:



      componentDidMount() {
      this.setState({isLoading: true});

      fetch('http://localhost:8080/api/tasks/')
      .then(response => { return response.json() })
      .then(results => this.setState({
      tasks: results,
      isLoading: false
      }));


      The spring api which return data:



          @CrossOrigin(origins = "http://localhost:3000")
      @GetMapping
      public List<Task> findTasksByUserId() {
      return taskService.findTasksBelongToUser(idProvider.getCurrentUserId());
      }


      Returned JSON:



      [
      {
      id: 1,
      version: 0,
      name: "Task1",
      description: "description1",
      priority: 1,
      finished: true,
      category: {
      id: 1,
      version: 0,
      name: "Uncategorized",
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      }
      },
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      },
      position: 0
      },
      {
      id: 2,
      version: 0,
      name: "Task2",
      description: "description2",
      priority: 4,
      finished: true,
      category: {
      id: 1,
      version: 0,
      name: "Uncategorized",
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      }
      },
      user: {
      id: 1,
      version: 0,
      login: "admin",
      email: "admin@todo.pl",
      enabled: true
      },
      position: 1
      }]


      And i got a error like this:



      Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0


      I dont know what is wrong but maybe format of json is not correct, because it's start with the sign '['.
      I am a beginner in React so please about some hint and help.
      regards







      json spring reactjs api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:18









      AdrianAdrian

      328




      328
























          1 Answer
          1






          active

          oldest

          votes


















          1














          fetch() returns a promise which gets resolved into an HTTP response of course, not the actual JSON. To extract the JSON body content from the response, we use the json() method.



          Try adding two headers Content-Type and Accept to be equal to application/json.



          fetch('http://localhost:8080/api/tasks/', {
          headers : {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
          }

          })
          .then(response => { return response.json() })
          .then(results => this.setState({
          tasks: results,
          isLoading: false
          }));





          share|improve this answer


























          • Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

            – Adrian
            Nov 14 '18 at 18:38











          • what problem? If the tasks is an array map should work

            – Shivam
            Nov 14 '18 at 18:39













          • hmm that's weird because like i show above json is an array with objects

            – Adrian
            Nov 14 '18 at 18:40











          • If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

            – Shivam
            Nov 14 '18 at 18:43













          • No i am not yet, i dont't know why map doesnt fit

            – Adrian
            Nov 14 '18 at 18:46











          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',
          autoActivateHeartbeat: false,
          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%2f53306491%2fspring-react-fetch-data%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









          1














          fetch() returns a promise which gets resolved into an HTTP response of course, not the actual JSON. To extract the JSON body content from the response, we use the json() method.



          Try adding two headers Content-Type and Accept to be equal to application/json.



          fetch('http://localhost:8080/api/tasks/', {
          headers : {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
          }

          })
          .then(response => { return response.json() })
          .then(results => this.setState({
          tasks: results,
          isLoading: false
          }));





          share|improve this answer


























          • Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

            – Adrian
            Nov 14 '18 at 18:38











          • what problem? If the tasks is an array map should work

            – Shivam
            Nov 14 '18 at 18:39













          • hmm that's weird because like i show above json is an array with objects

            – Adrian
            Nov 14 '18 at 18:40











          • If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

            – Shivam
            Nov 14 '18 at 18:43













          • No i am not yet, i dont't know why map doesnt fit

            – Adrian
            Nov 14 '18 at 18:46
















          1














          fetch() returns a promise which gets resolved into an HTTP response of course, not the actual JSON. To extract the JSON body content from the response, we use the json() method.



          Try adding two headers Content-Type and Accept to be equal to application/json.



          fetch('http://localhost:8080/api/tasks/', {
          headers : {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
          }

          })
          .then(response => { return response.json() })
          .then(results => this.setState({
          tasks: results,
          isLoading: false
          }));





          share|improve this answer


























          • Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

            – Adrian
            Nov 14 '18 at 18:38











          • what problem? If the tasks is an array map should work

            – Shivam
            Nov 14 '18 at 18:39













          • hmm that's weird because like i show above json is an array with objects

            – Adrian
            Nov 14 '18 at 18:40











          • If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

            – Shivam
            Nov 14 '18 at 18:43













          • No i am not yet, i dont't know why map doesnt fit

            – Adrian
            Nov 14 '18 at 18:46














          1












          1








          1







          fetch() returns a promise which gets resolved into an HTTP response of course, not the actual JSON. To extract the JSON body content from the response, we use the json() method.



          Try adding two headers Content-Type and Accept to be equal to application/json.



          fetch('http://localhost:8080/api/tasks/', {
          headers : {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
          }

          })
          .then(response => { return response.json() })
          .then(results => this.setState({
          tasks: results,
          isLoading: false
          }));





          share|improve this answer















          fetch() returns a promise which gets resolved into an HTTP response of course, not the actual JSON. To extract the JSON body content from the response, we use the json() method.



          Try adding two headers Content-Type and Accept to be equal to application/json.



          fetch('http://localhost:8080/api/tasks/', {
          headers : {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
          }

          })
          .then(response => { return response.json() })
          .then(results => this.setState({
          tasks: results,
          isLoading: false
          }));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 5:32

























          answered Nov 14 '18 at 18:28









          ShivamShivam

          1,1121230




          1,1121230













          • Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

            – Adrian
            Nov 14 '18 at 18:38











          • what problem? If the tasks is an array map should work

            – Shivam
            Nov 14 '18 at 18:39













          • hmm that's weird because like i show above json is an array with objects

            – Adrian
            Nov 14 '18 at 18:40











          • If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

            – Shivam
            Nov 14 '18 at 18:43













          • No i am not yet, i dont't know why map doesnt fit

            – Adrian
            Nov 14 '18 at 18:46



















          • Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

            – Adrian
            Nov 14 '18 at 18:38











          • what problem? If the tasks is an array map should work

            – Shivam
            Nov 14 '18 at 18:39













          • hmm that's weird because like i show above json is an array with objects

            – Adrian
            Nov 14 '18 at 18:40











          • If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

            – Shivam
            Nov 14 '18 at 18:43













          • No i am not yet, i dont't know why map doesnt fit

            – Adrian
            Nov 14 '18 at 18:46

















          Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

          – Adrian
          Nov 14 '18 at 18:38





          Ok, but now i have a problem with function map in this line {tasks.map(task => ( <li key={task.id}> {task.name} </li> ))}

          – Adrian
          Nov 14 '18 at 18:38













          what problem? If the tasks is an array map should work

          – Shivam
          Nov 14 '18 at 18:39







          what problem? If the tasks is an array map should work

          – Shivam
          Nov 14 '18 at 18:39















          hmm that's weird because like i show above json is an array with objects

          – Adrian
          Nov 14 '18 at 18:40





          hmm that's weird because like i show above json is an array with objects

          – Adrian
          Nov 14 '18 at 18:40













          If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

          – Shivam
          Nov 14 '18 at 18:43







          If its array of objects then the function passed as argument to map will be executed for all objects in the array. Did you resolve your issue?

          – Shivam
          Nov 14 '18 at 18:43















          No i am not yet, i dont't know why map doesnt fit

          – Adrian
          Nov 14 '18 at 18:46





          No i am not yet, i dont't know why map doesnt fit

          – Adrian
          Nov 14 '18 at 18:46




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306491%2fspring-react-fetch-data%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python