+ 5

How do I resolve CORS issue?

I've been trying to create a new item through Angular and used PHP as my backend, but I kept receiving an error saying, "Access to XMLHttpRequest at 'http://localhost/api/product/create.php' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status." These are the headers: header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8") header("Access-Control-Allow-Methods: POST") header("Access-Control-Allow-Age: 3600") header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With); It works as expected when I tested it on Postman. The solutions I find online doesn't work for me. How do I resolve this issue?

9th Jan 2019, 3:56 PM
silentlearner
silentlearner - avatar
6 Answers
+ 6
David Carroll No, it doesn't require authentication. And honestly, I don't remember what I was doing with this code, so I can't update you with more information. During that time, I decided to rebuild the whole thing without the Angular framework and it worked fine. But still, thanks for answering this old question. I'm sure your answer would be useful if anyone else encountered a similar problem. I can't mark it as the best/right answer though because I don't know if it would have solved the issue I was facing.
21st Mar 2019, 5:56 PM
silentlearner
silentlearner - avatar
+ 3
You can use jsonp or set a http header like this Access-Control-Allow-Origin: https://www techslaim.com Access-Control-Allow-Methods: POST, GET Access-Control-Allow-Headers: X-PINGOTHER, Content-Type Access-Control-Max-Age: 86400
9th Jan 2019, 5:09 PM
Adarsh.n. Bidari
Adarsh.n. Bidari - avatar
+ 3
Can you check postman headers, maybe they can give you an idea how to resolve it?
17th Mar 2019, 3:07 PM
Dejan Dozet
Dejan Dozet - avatar
+ 3
This works with Postman because Postman isn't bound by the same security restrictions found within modern browsers today. Is this on a session requiring authentication? If so, try adding another header: header ("Access-Control-Allow-Credentials: true"); When checking your network requests in developer web tools, are you seeing the OPTION request as your preflight? Are you setting the following headers for that OPTION response: Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Allow-Headers: Content-Type I can explain more steps to explore after getting your update on this request. It's 3:23am here, so, I may not respond until tomorrow.
18th Mar 2019, 7:24 AM
David Carroll
David Carroll - avatar
0
Please am having this same issue and I can't tell my team to write the code in another language..... also have tried almost everything on Google it's not working David Carroll Can you assist? Can't I create the issue and put my source code? [Edited by David Carroll] Ayodele Abigail Oluwabunmi Post a new question with a link to your code if possible. Mention me in that new question and I will take a look when I can.
15th Oct 2019, 7:20 PM
Ayodele Abigail Oluwabunmi
Ayodele Abigail Oluwabunmi - avatar
0
Hello everyone , I am using front end as Angular 7 and back end as PHP 7 with framework code egnitor , now I am stuck with Cors request . I am unable to figure out from which site the request is not hitting as earlier it hit once in PHP 5 but after some shift in PHP 7 it is not working so unable to figure out where the problem lie as I used all the header authentication and also there are some error in angular part so can anyone list me steps from angular and codeigniter part perspective so that or debugging steps from both side so that I can debug from where the problem lies
16th Jan 2021, 3:54 AM
radhika jindal
radhika jindal - avatar