0

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");
user1687891
  • 570
  • 4
  • 22

3 Answers3

1

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.

nas
  • 1,840
  • 2
  • 19
  • 41
0

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

dc-p8
  • 706
  • 1
  • 5
  • 18
  • Yes, and it only allow origins. Everybody is authorized to send a request. But the question is : What are the HTTP verbs that are open for that user ? This is why i think you need Access-Control-Allow-Methods – dc-p8 Apr 19 '18 at 08:54
  • I have header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); – user1687891 Apr 19 '18 at 09:00
0

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:

Zamrony P. Juhara
  • 4,834
  • 2
  • 22
  • 37