Отправка POST запроса React Native
Помогите разобраться c отправкой POST запроса из формы. Я только начал изучать React Native, сильно не пинайте за код. Вот что есть сейчас:
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useCallback } from 'react';
import { SafeAreaView, StyleSheet, Text, View, TextInput, Button, Alert } from 'react-native';
import { useForm, Controller } from "react-hook-form";
export const NewTransactions = props => {
const { register, handleSubmit, setValue } = useForm();
const onSubmit = useCallback(formData => {
fetch('url',{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
formData
})
}).then(response=>response.json() )
console.log(formData);
}, []);
const onChangeField = useCallback(
name => text => {
setValue(name, text);
},
[]
);
useEffect(() => {
register('category');
register('count');
register('date');
register('comment');
register('type');
}, [register]);
return (
<SafeAreaView style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<TextInput
autoCompleteType="off"
keyboardType="default"
textContentType="none"
placeholder="Категория"
onChangeText={onChangeField('category')}
/>
<TextInput
autoCompleteType="off"
keyboardType="decimal-pad"
textContentType="none"
placeholder="Сумма"
onChangeText={onChangeField('count')}
/>
<TextInput
autoCompleteType="off"
keyboardType="default"
textContentType="none"
placeholder="Дата"
onChangeText={onChangeField('date')}
/>
<TextInput
autoCompleteType="off"
keyboardType="default"
textContentType="none"
placeholder="Комментарий"
onChangeText={onChangeField('comment')}
/>
<TextInput
autoCompleteType="off"
keyboardType="default"
textContentType="none"
placeholder="Тип транзакции"
onChangeText={onChangeField('type')}
/>
<Button title="Сохранить" onPress={handleSubmit(onSubmit)} />
</SafeAreaView>
);
};
В ответ я получаю ошибку
[Unhandled promise rejection: SyntaxError: JSON Parse error: Unable to parse JSON string]
at [native code]:null in parse
at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue
На сервере куда отправляется запрос все работает. Проверял PostMan`ом