So i'm trying to post some data in my database from Angular (the backend is in PHP) using HTTPCLIENT.
The data that I want to post looks like this :
userData = { name: '', address: '', uname: '', pwd: '' };
The service that handles the request :
registerUser(userData): Observable<any> {
return this.http.post(this.api.buildEndpoint('auth/register.php'), JSON.stringify(userData), this.api.httpOptions);
}
And I have a api.ts file which contains everything related to the api
private readonly API_URI = 'http://localhost/users/';
public httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
})
};
/// buildEndpoint() method
When I inspect the Network tab in the Developer Tools I see that there are two requests :
- a OPTIONS request (which does not have a body)
- and the actual POST request which contains the user information
This is normal behavior I guess, but the issue is that when I check my database I find two inserted rows , it's like the OPTIONS method had a body and posted something in the database.
I don't have this issue when I post data using Postman for example, only from the actual frontend (Angular)
I don't know what's wrong exactly, help is needed thanks !