简体   繁体   English

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

[英]JSON Parse error: Unexpected identifier “Try”

So I am following this guide and my web host works, but a client still get an error: 因此,我正在遵循本指南,并且我的虚拟主机可以正常工作,但是客户端仍然出现错误:

JSON Parse error: Unexpected identifier "Try"

This is how my register.js code looks like: 这是我的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;

I tried to make a post request on your mentioned url https://lifestormweb.000webhostapp.com/user_registration.php 我试图在您提到的网址上发帖请求https://lifestormweb.000webhostapp.com/user_registration.php

and I get this response 我得到这个回应 在此处输入图片说明

which is not a json, so you need to do the handling for such responses in your code, hope it helps!! 这不是json,因此您需要在代码中对此类响应进行处理,希望对您有所帮助!!

The response you are fetching is a string value not JSON.You need convert the response maybe like: 您获取的响应是一个字符串值而不是JSON。您需要将响应转换为:

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

Code will verify if the server side response don't have issues. 代码将验证服务器端响应是否没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM