javascript - JSON 解析错误 : Unexpected identifier "Try"

标签 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;

最佳答案

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

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

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

关于javascript - JSON 解析错误 : Unexpected identifier "Try",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51206761/

相关文章:

javascript - 如何告诉 TypeScript Array 中没有 undefined?

javascript - 将元素粘贴到滚动固定元素的底部?

javascript - React 16错误边界组件(使用componentDidCatch)显示 Uncaught Error

javascript - jQuery - 使用嵌套 setTimeout 暂停 for 循环

javascript - 使用 chrome 时,我网站上的 Youtube 嵌入式视频无法全屏显示

jquery - 如何使用 jquery.min 与 webpack 进行 react

javascript - 如何将空数组放入模板字符串

javascript - 如何在 react-native 中禁用 ssl 证书验证

ios - 如何在 React Native 上运行 Facebook 的示例? ["AwesomeProject"未找到]

ios - 拨出电话的 CallKit UI