When I tried to publish data from native-native to the PHP API, response-native will show an error:
Json Parse Error: Unrecognized token '<'
I tested the PHP API with a postman with the header type 'application / json', it works fine, here is the code responsible for the reaction, can anyone help me with this? Thanks in advance!
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, ActivityIndicatorIOS, TextInput, TouchableOpacity, } from 'react-native'; const REQUEST_URL = 'http://localhost:8000/user'; export default class extends Component { constructor(props) { super(props); } _submit() { fetch(REQUEST_URL, { method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ firstname: "Justin", lastname: "Robot" }) }) .then((response) => response.json()) .then((responseData) => { console.log(responseData.body); }) .done(); } render() { return ( <View style={styles.container}> <TouchableOpacity style={styles.submitButton} onPress={() => this._submit()} > <Text>http post</Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, submitButton: { backgroundColor: 'lightskyblue', borderRadius: 5, paddingTop: 5, paddingBottom: 5, paddingLeft: 20, paddingRight: 20, } });
source share