JSON解析错误:意外的标识符“尝试”

时间:2018-07-06 09:05:07

标签: javascript reactjs react-native react-native-ios

因此,我正在关注this guide,并且我的虚拟主机可以正常工作,但是客户端仍然出现错误:

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;

2 个答案:

答案 0 :(得分:0)

我试图对您提到的网址https://lifestormweb.000webhostapp.com/user_registration.php发出发帖请求

然后我收到此回复enter image description here

这不是json,因此您需要在代码中对此类响应进行处理,希望对您有所帮助!

答案 1 :(得分:0)

您要获取的响应是一个字符串值而不是JSON。您需要将响应转换为:

{"result": "Something went wrong.Try again", code: "500"}

代码将验证服务器端响应是否没有问题。