I am trying to post data to server. My php side is working fine. When I tried using postman post is working. But from angular I am getting 405 (Method Not Allowed) error:
zone.js:2935 OPTIONS http://angularslim.local/public_html/users 405 (Method Not Allowed)
Failed to load http://angularslim.local/public_html/users: Response for preflight has invalid HTTP status code 405
.
My codes are below: Inside my service I have following code.
@Injectable()
export class AuthService {
constructor(private http: Http) {}
register(user:User){
this.http.post("http://angularslim.local/public_html/users", user).subscribe((res: Response) => {
console.log("inside");
})
}
And in my php section I do have these lines
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
Did you try passing the form value by JSON.stringify(user). I had a same issue before. Later I found that my problem was that.
this.http.post("your api url", JSON.stringify(user))
.subscribe(
(val) => {
console.log("POST call successful value returned in body", val);
});
Hope this works for you.
You need to allow cross origins AND methods
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
Also try this https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
In addition, i suggest you to use HttpClient (http is deprecated) and HttpResponse
If you use Slim, this is mostly because your Javascript code sends HTTP OPTIONS request while there is no route that handle HTTP OPTIONS.
You need to add a route that handle OPTIONS request or modify any Javascript code that cause preflight request to be sent (by making it a simple request. Take a look at this question Why is an OPTIONS request sent and can I disable it?).
To add route that handle OPTIONS
$app->options('/users', function ($request, $response, $args) {
//do something here
]);
More information: