Php React Native选择单个json_编码字符串,但不选择数组
我试图从PHP返回数组,通过Fetch进行本地响应。只要返回的变量是单个变量,它就可以正常工作,但当我返回数组时,响应将变为空 我的PHP代码Php React Native选择单个json_编码字符串,但不选择数组,php,react-native,fetch,Php,React Native,Fetch,我试图从PHP返回数组,通过Fetch进行本地响应。只要返回的变量是单个变量,它就可以正常工作,但当我返回数组时,响应将变为空 我的PHP代码 header("Access-Control-Allow-Origin: *"); if(isset($_POST) && !empty($_POST)){ }else{ parse_str($_SERVER['REDIRECT_QUERY_STRING'], $get_array);
header("Access-Control-Allow-Origin: *");
if(isset($_POST) && !empty($_POST)){
}else{
parse_str($_SERVER['REDIRECT_QUERY_STRING'], $get_array);
$_GET = (array) $get_array;
$_POST = (array) json_decode(file_get_contents('php://input'));
}
$to_return = array();
if(isset($_GET['token']) && $_GET['token'] != ''){
if(check_token($_GET['token'])){
if(isset($_POST['email']) && $_POST['email'] != '' && isset($_POST['password']) && $_POST['password'] != ''){
$user = wp_authenticate( $_POST['email'], $_POST['password'] );
if($user){
$token = md5(uniqid(rand(), true));
delete_user_meta( $user->ID, 'api_token');
add_user_meta( $user->ID, 'api_token', $token);
$to_return['token'] = $token;
$to_return['result'] = 'user logged in successfully';
}else{
$to_return['result'] = 'user not authorized';
}
}else{
$to_return['result'] = 'parameters are missing2';
}
}else{
$to_return['result'] = 'invalid token';
}
}else{
$to_return['result'] = 'parameters are missing1';
}
//this line works fine
echo json_encode($to_return['token']);
//this line gives emoty response
echo json_encode($to_return);
我的本地代码:
import React from 'react';
import { View, Text, Button, TextInput, AsyncStorage, StyleSheet } from 'react-native';
import FormData from 'FormData';
class LoginScreen extends React.Component {
login = () => {
url = 'https://foodtest.cloudaccess.host/wp-json/v1/login_customer?token=********';
var object = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: '**********@network-source.com',
password: '*******',
})
};
fetch(url, object)
.then((response) => response.json())
.then((responseData) => { console.log(responseData); })
.catch((err) => { console.log(err); });
}
render() {
return (
<View style={styles.container}>
<Text>Login Screen</Text>
<View style={styles.field_cont}>
<Text>Email</Text>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1, width: '100%' }}
/>
</View>
<View style={styles.field_cont}>
<Text>Password</Text>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
/>
</View>
<Button
title="Login"
color="#841584"
accessibilityLabel="Login"
onPress={this.login}
/>
</View>
);
}
}
export default LoginScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
width: '100%',
height: 100,
justifyContent: 'center',
alignItems: 'center'
},
field_cont: {
padding: 10,
width: '100%'
}
});
我正在使用Reactotron查看返回值。在PHP中,第二行显示为空,并且成功显示单个变量
这可能是因为JSON的解析很奇怪,请尝试response.text并查看JSON字符串如何返回
还要为content-type-application/json添加一个PHP头,看看这是否会有所不同 请澄清一下,错误是当您尝试在React Native上处理响应时,还是当您尝试在PHP上发送响应时?错误发生在我将PHP中的值从单个变量更改为数组时。如果我回显json_encode single_变量,则可以返回变量并在控制台中显示。但当我将其更改为echo json_encode Array时,响应将变为空。我想从PHP返回一个数组来React Native,这就是我想要做的。那么你的问题是PHP而不是React Native,或者我错了?PHP与此无关。它返回数组,但React Native无法读取它。阅读上面的内容,你就会得到它。response.text是否返回任何内容?上面的答案在PHP代码中也是一个错误,并且你需要返回json_编码。问题是我在重复它。谢谢你的帮助!!!