skip to Main Content

I’m developing an application in which I make an image upload request.

And it gives me the following error when sending the image for upload:

Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.

Below is a snippet of the code in Angular:

const req = new HttpRequest('POST', `${this.baseUrl}/uploads/upload.php`, formData, {
      reportProgress: true,
      responseType: 'json'
    });

    return this.http.request(req);

Below is a snippet of the code in PHP:

header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
    header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE');
    header('Content-Type: application/json; charset=utf-8');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Max-Age: 60");
    header("HTTP/1.1 200 OK");
    header("HTTP/2 200 OK");
    header("Status 200");

Does anyone have a possible solution for this BUG?

2

Answers


  1. Access-Control-Allow-Origin: *
    add this line in header.

    Login or Signup to reply.
  2. I believe you’ll need to add OPTIONS to the allowed methods, and the name of your domain to allowed origin. Make sure the the target server is setup to return the OPTIONS method in the header of the preflight response.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search