При попытке отправки fetch запроса на React на локальный машину выводит ошибку: SyntaxError: Unexpected end of input.
Сам код (React):
import React from "react";
const Test = () => {
const [answer, setAnswer] = React.useState([]);
const [text1, setText1] = React.useState("");
const [text2, setText2] = React.useState("");
const fetchs = (e) => {
e.preventDefault();
const userData = {
text1,
text2,
};
fetch('https://testc1/index.php', {
method: "POST",
mode: "no-cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userData),
})
.then((res) => {
return res.json();
})
.then((result) => {
setAnswer(result);
console.log(result);
})
.catch((error) => {
console.error("Error:", error);
});
};
return (
<form onSubmit={fetchs} method="POST">
<input
type="text"
name="text1"
id="text1"
placeholder="text1"
value={text1}
onChange={(e) => setText1(e.target.value)}
/>
<input
type="text"
name="text2"
id="text2"
placeholder="text2"
value={text2}
onChange={(e) => setText2(e.target.value)}
/>
<button type="submit">send</button>
<p>{answer}</p>
</form>
);
};
export default Test;
Код index.php (на локалке):
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$dataGet = file_get_contents("php://input");
$data = json_decode($dataGet);
if (!empty($data)) {
$resp = [
'status' => 'success',
'errors' => 'zero'
];
echo json_encode($resp);
}