Ionic http request to php server cause CORS error

has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

    let body = new FormData();
    let head=new Headers();
    var header = { "headers": {"Content-Type": "application/json"} };
    var options = new RequestOptions();
    options.headers = new Headers();
    options.headers.append('Content-Type', 'application/json');
    options.headers.append('X-Requested-With', 'XMLHttpRequest');
    body.append('cmd', "get_page_ad_list");
    body.append('code', "wouienfjnfyen3wf42hnlks#$@#23");

I added server code header(“Access-Control-Allow-Origin: *”);
but having same error…

how can I solve problem…

I’m not sure I can answer your exact question, but here are some observations:

  • You’re specifying Content-Type twice
  • Both times you’re lying, because it’s actually application/x-www-form-urlencoded
  • You shouldn’t be setting it at all, because Angular’s HttpClient does that for you
  • In order to minimize CORS problems, set as few headers as possible
  • Specifically, do not set any X-foobar-type things
  • There’s no decent reason to use http endpoints any more - use https

I have similar problem in php, because i left space before php tag so i removed space and it’s working fine