Spring + React fetch data
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
add a comment |
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
add a comment |
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
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
json spring reactjs api
asked Nov 14 '18 at 18:18
AdrianAdrian
328
328
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
));
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 thetasks
is an arraymap
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
|
show 7 more comments
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
);
);
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%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
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
));
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 thetasks
is an arraymap
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
|
show 7 more comments
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
));
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 thetasks
is an arraymap
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
|
show 7 more comments
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
));
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
));
edited Nov 15 '18 at 5:32
answered Nov 14 '18 at 18:28
ShivamShivam
1,1271231
1,1271231
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 thetasks
is an arraymap
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
|
show 7 more comments
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 thetasks
is an arraymap
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
|
show 7 more comments
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.
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%2f53306491%2fspring-react-fetch-data%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