英文:
React Fetch post api return response [object Object]?
问题
const App = () => {
const [userInfo, setuserInfo] = useState('');
useEffect(() => {
const url = 'https://test.com/v1/getSessionInfoById';
const requestOptions = {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify({ sessionId: 'cf209972-51d6-37d5-b9e9' })
};
const fetchData = async () => {
try {
const response = await fetch(url, requestOptions);
const json = await response.json();
console.log("json " + JSON.stringify(json));
setuserInfo(json);
} catch (error) {
console.log("error", error);
}
};
fetchData();
}, []);
return (
<>{JSON.stringify(userInfo)}</>
)
};
export default App;
英文:
I am working on react. I have an api that is using POST method, body contains sessionId
and returns response data in json. In postman, it's working fine. But in React, its returning [object Object]
. Method is post, but api is getting the data. Please let me know what's the issue why console.log("json " + json)
is giving [object Object]
const App = () => {
const [userInfo, setuserInfo] = useState(‘’);
useEffect(() => {
const url = 'https://test.com/v1/getSessionInfoById';
const requestOptions = {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify({ sessionId: 'cf209972-51d6-37d5-b9e9' })
};
const fetchData = async () => {
try {
const response = await fetch(url, requestOptions);
const json = await response.json();
console.log("json " + json);
setuserInfo(json);
} catch (error) {
console.log("error", error);
}
};
fetchData();
}, []);
return (
<>userInfo</>
)
};
export default App;
Postman response is
{
"sessionId": "cf209972-51d6-37d5-b9e9",
"userId": "114",
"expirationDate": "2023-04-21",
"keyPropertiesList": [
{
"id": 266277,
"properties": {
"user": {
"firstName": "test",
"lastName": "A",
"fullName": "test A"
},
"userDetail": {
"phoneNo": "666-777-9999",
"email": "test@test.com"
},
"locationID": "78"
}
}
]
}
答案1
得分: 1
The api is giving the correct answer however, you are wrongly printing it.
const js = { name: "sachin" }
console.log("json", js)
Look at this code once, I wrote the right json but still it says [Object Object] because I'm printing in the console with ,
operator as: console.log("json", js)
Because you are not concatenating here, you are just printing those values.
英文:
The api is giving the correct answer however, you are wrongly printing it.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const js = { name : "sachin" }
console.log("json" + js )
<!-- end snippet -->
look at this code once, I wrote the right json bt still it says [Object Object] because I'm printing in the console with +
operator as : console.log("json" + js )
Because you are trying to concatenate a string with an object.
however replacing +
with ,
will give the correct answer.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const js = { name : "sachin" }
console.log("json" , js )
<!-- end snippet -->
Like this ( because you are not concatenating here, you are just printing those values )
答案2
得分: 1
response.json 返回解析后的 Json 作为一个 "Object"。您正在将此对象与一个字符串连接起来:
console.log("json " + json);
因此,您将获得字符串表示形式。您可能想要:
console.log("json ", json);
英文:
response.json returns the parsed Json as an "Object". You are concatenating this object with a string:
console.log("json " + json);
Hence you will get the String representation. You probably want:
console.log("json ", json);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论