Javascript JSON分析错误:意外标识符“0”;试一试;
因此,我遵循以下步骤,我的web主机工作正常,但客户端仍然会出错:Javascript JSON分析错误:意外标识符“0”;试一试;,javascript,reactjs,react-native,react-native-ios,Javascript,Reactjs,React Native,React Native Ios,因此,我遵循以下步骤,我的web主机工作正常,但客户端仍然会出错: JSON Parse error: Unexpected identifier "Try" 这就是我的register.js代码的样子: import React, { Component } from "react"; import { StyleSheet, Text, View, StatusBar, TouchableOpacity, Alert, TextInput } from "rea
JSON Parse error: Unexpected identifier "Try"
这就是我的register.js
代码的样子:
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
StatusBar,
TouchableOpacity,
Alert,
TextInput
} from "react-native";
import { navigation } from "react-navigation";
import Form from "../forms/Form";
export default class Register extends Component<{}> {
constructor(props) {
super(props);
this.state = {
UserName: "",
UserEmail: "",
UserPassword: ""
};
}
UserRegistrationFunction = () => {
const { UserName } = this.state;
const { UserEmail } = this.state;
const { UserPassword } = this.state;
fetch("https://lifestormweb.000webhostapp.com/user_registration.php", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
name: UserName,
email: UserEmail,
password: UserPassword
})
})
.then(response => response.json())
.then(responseJson => {
Alert.alert(responseJson);
})
.catch(error => {
console.error(error);
});
};
render() {
return (
<View style={styles.container}>
<TextInput
style={styles.inputBox}
underlineColorAndroid="#ffffff"
placeholder="Ihre Name"
placeholderTextColor="#ffffff"
selectionColor="#fff"
onChangeText={UserName => this.setState({ UserName })}
onSubmitEditing={() => this.password.focus()}
/>
<TextInput
style={styles.inputBox}
underlineColorAndroid="#ffffff"
placeholder="Ihre E-mail"
placeholderTextColor="#ffffff"
selectionColor="#fff"
keyboardType="email-address"
onChangeText={UserEmail => this.setState({ UserEmail })}
onSubmitEditing={() => this.password.focus()}
/>
<TextInput
style={styles.inputBox}
underlineColorAndroid="#ffffff"
placeholder="Passwort"
secureTextEntry={true}
placeholderTextColor="#ffffff"
onChangeText={UserPassword => this.setState({ UserPassword })}
ref={input => (this.password = input)}
/>
<TouchableOpacity
onPress={this.UserRegistrationFunction}
style={styles.button}
>
<Text style={styles.buttonText}>Sich anmelden</Text>
</TouchableOpacity>
<View style={styles.signupTextCont}>
<Text style={styles.signupText}>Haben Sie schon einen Account?</Text>
<TouchableOpacity
onPress={() => this.props.navigation.navigate("Login")}
>
<Text style={styles.signupButton}> Sich einloggen</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
module.exports = Register;
import React,{Component}来自“React”;
进口{
样式表,
文本,
看法
状态栏,
可触摸不透明度,
警觉的,
文本输入框
}从“反应本族语”;
从“反应导航”导入{navigation};
从“./表格/表格”导入表格;
导出默认类寄存器扩展组件{
建造师(道具){
超级(道具);
此.state={
用户名:“”,
用户电子邮件:“”,
用户密码:“
};
}
UserRegistrationFunction=()=>{
const{UserName}=this.state;
const{UserEmail}=this.state;
const{UserPassword}=this.state;
取回(“https://lifestormweb.000webhostapp.com/user_registration.php", {
方法:“张贴”,
标题:{
接受:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify({
名称:用户名,
电子邮件:UserEmail,
密码:UserPassword
})
})
.then(response=>response.json())
.然后(responseJson=>{
警惕,警惕(responseJson);
})
.catch(错误=>{
控制台错误(error);
});
};
render(){
返回(
this.setState({UserName})}
onSubmitEditing={()=>this.password.focus()}
/>
this.setState({UserEmail})}
onSubmitEditing={()=>this.password.focus()}
/>
this.setState({UserPassword})}
ref={input=>(this.password=input)}
/>
西奇安梅尔登
你有帐户吗?
this.props.navigation.navigate(“Login”)}
>
西希·艾恩洛根
);
}
}
module.exports=寄存器;
我试图对您提到的url发出post请求https://lifestormweb.000webhostapp.com/user_registration.php
我得到了这样的回应
这不是json,所以您需要在代码中处理此类响应,希望它能有所帮助 您正在获取的响应是字符串值而不是JSON。您需要将响应转换为以下格式:
{"result": "Something went wrong.Try again", code: "500"}
代码将验证服务器端响应是否没有问题。它说这是一个JSON解析错误……因此您需要查看您试图解析的JSON!您很可能无法从
https://lifestormweb.000webhostapp.com/user_registration.php
。很明显,您试图将文本解析为非JSON的JSON;可能是错误信息或类似信息。另外(可能是相关的,但可能不是),您错过了对fetch
调用的一个重要检查:在使用response.json
之前,您需要检查response.ok
。更多信息请参见我的博客帖子。