1

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:

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;
3
  • 3
    It says it was a JSON parse error … so you need to look at the JSON you are trying to parse! Commented Jul 6, 2018 at 9:06
  • You are most likely not getting JSON back from your request to https://lifestormweb.000webhostapp.com/user_registration.php. Commented Jul 6, 2018 at 9:08
  • 1
    Clearly, you're trying to parse text as JSON that isn't JSON; perhaps it's an error message or similar. Also (and perhaps related, but perhaps not), you're missing out an important check on that fetch call: You need to check response.ok before using response.json. More in my blog post here. Commented Jul 6, 2018 at 9:08

2 Answers 2

1

The response you are fetching is a string value not JSON.You need convert the response maybe like:

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

Code will verify if the server side response don't have issues.

Sign up to request clarification or add additional context in comments.

Comments

0

I tried to make a post request on your mentioned url https://lifestormweb.000webhostapp.com/user_registration.php

and I get this responseenter image description here

which is not a json, so you need to do the handling for such responses in your code, hope it helps!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.