0

I am developing a Web application using Angular. Backend API is developed using Laravel PHP framework. I enabled the CORS access creating a middleware in the Laravel.

This is the Laravel middleware

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
        ->header('Access-Control-Allow-Headers','X-Requested-With,content-type')
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

I can successfully make the GET request like this.

this.http.get<IEvent[]>(this.globals.API_ENDPOINT + "event/list?email=" + email).subscribe(data=> {
    //do something with the data
});

The above GET request is working fine. But the problem is with making POST request with some parameters.

I send the POST request like this.

var formData = {
   name: "My name"
}
this.http.post<ISimpleForm>(this.globals.API_ENDPOINT + "event/post", formData).subscribe(data=>{

})

As you can see the formData variable is object. When I send the request, I got this error in console.

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 

I tried lots of solution online. For example, passing headers with "Content-Type" with "application/json" and so on. None of them helped. I already solved CORS issue in the server side as well.

But the request is working when I send the body it as string like this

this.http.post<ISimpleForm>(this.globals.API_ENDPOINT + "event/post", formData).subscribe(data=>{

    })

The CORS error is gone when I stringify the body. But the problem is when I log the request data in the server side using $_POST, nothing is passed to the request. So the body is empty.

So, how can I make post request with some request parameters using HttpClient in Angular? What is wrong with my code and how can I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • related: https://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working?rq=1 – Jeff Apr 09 '18 at 10:52
  • it looks like (in the non-working version) the server doesn't respond correctly to the `OPTIONS` preflight request, which your browser will do _before_ the actual `POST` request. – Jeff Apr 09 '18 at 10:53
  • suggesting this https://github.com/barryvdh/laravel-cors insted middleware – vishal pardeshi Apr 09 '18 at 10:54
  • use proxy in angular check this post https://stackoverflow.com/q/37172928/3898339 – Deep 3015 Apr 09 '18 at 10:55
  • I tried, it still does not work @Deep3015 – Wai Yan Hein Apr 09 '18 at 11:02
  • Hi, @vishalpardeshi, I am using a CORS middleware in laravel. I believe mine works because when I made get request, i was getting cors error. I solved it by adding my CORS middleware. – Wai Yan Hein Apr 09 '18 at 11:03
  • 1
    Hello, @Wai Yan Hein but i got same error a will ago. this middleware works for methods not for preflight header. chick this https://stackoverflow.com/questions/34748981/laravel-5-2-cors-get-not-working-with-preflight-options – vishal pardeshi Apr 09 '18 at 11:11
  • 1
    @vishalpardeshi. You are right. The middleware I was using ealier was not working properly. When I used your one, every is working now. Thanks so much. – Wai Yan Hein Apr 09 '18 at 11:44

1 Answers1

1

I got the same error a while ago. this middleware works for methods not for preflight header. Check this Laravel 5.2 CORS, GET not working with preflight OPTIONS

Suggesting this to use in your project github.com/barryvdh/laravel-cors instead of middleware.

halfer
  • 19,824
  • 17
  • 99
  • 186
vishal pardeshi
  • 334
  • 4
  • 14