[ad_1]
I know there are a lot of similar questions but none of them met my needs. I’m trying to receive a JSON object in my PHP backend from the react frontend.
BackendHelper.tsx
const BACKEND_API = 'https://URL_TO_BACKEND/?action=';
export async function post(endpoint: string, data: unknown) {
let response = await fetch(BACKEND_API + endpoint);
await fetch(BACKEND_API + endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
body:JSON.stringify(data),
});
return await response.json();
}
I have another class to insert the parameters I need
LogLogin.tsx
import {post} from './BackendHelper';
export class LogLogin {
public static log(username: FormDataEntryValue, password: FormDataEntryValue): Promise<unknown> {
return post('logLogin', {username: username, password: password});
}
In loginForm.tsx
I have this line of code await LogLogin.log(data.get('email'), data.get('password'));
to perform the function.
In the backend I try to get it like that:
$data = json_decode(file_get_contents('php://input'), true);
And since it always returned only an int 1
I found out that with json_last_error
it returned a Syntax error, malformed JSON React
I don’t really see how a malformed JSON object gets created with my current code 🙂
[ad_2]