通常,我会在React Native中使用reponseJson
来访问JSON的响应,但是如何访问多个响应呢?
我把它从php json_encoded
这里:
Login.php
if ($row = mysqli_fetch_array($result)) {
$allresults = array( 'Data Matched' => $objA, "Hello" => $objB );
$Success = 'Data Matched';
// Converting the message into JSON format.
$Success = json_encode($allresults);
// Echo the message.
echo $Success ;
} else {}
当我试图通过responseJson.objA
访问它时,我得到了这个错误:[object Object]
in React Native:
Login = () =>{
this.setState({ loading: true, disabled: true }, () =>{
fetch('http://www.exmaple.com/React/login.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
})
}).then((response) => response.json())
.then((responseJson) => {
// If server response message same as Data Matched
if(responseJson.objA === 'Data Matched')**<-- Want the first response to go here**
{
this.dropdown.alertWithType('success', 'Login Success', reponseJson.objB); **<-- Want the second response to go here**
}
}).catch((error) => {
console.log(error);
});
我从SO,我做错了什么?multiple JSON object response跟随这个问题。
发布于 2018-10-19 07:56:07
您可能希望将此代码放在react本机代码中的if语句之前,
alert(JSON.stringify(responseJson, null, 1));
所以你可以看到字符串形式的响应。
https://stackoverflow.com/questions/52884004
复制相似问题