In order to solve this problem from my PHP script, I added the following code at the beginning of my PHP code:
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS, PATCH, DELETE');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Authorization, Content-Type, x-xsrf-token, x_csrftoken, Cache-Control, X-Requested-With');
header('Access-Control-Max-Age: 86400');
To verify that actual headers are coming or not, I used the curl command to test the performance of my URL using the following command:
curl -i https://dakshalab.com/dakshalab/admin/api.php?apicall=test_list
The output of this command is as follows:
HTTP/2 200
content-type: application/json
access-control-max-age: 86400
set-cookie: PHPSESSID=788b6708ed9ce77552e97f517cc3b119; path=/; secure
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate
pragma: no-cache
access-control-allow-origin: *
access-control-allow-methods: GET, POST, OPTIONS, DELETE, PUT
access-control-allow-headers: AccountKey,x-requested-with, Content-Type, origin, authorization, accept, client-security-token, host, date, cookie, cookie2
access-control-allow-credentials: true
date: Wed, 09 Aug 2023 07:43:35 GMT
server: LiteSpeed
alt-svc: h3=":443"; ma=2592000, h3-29=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q043=":443"; ma=2592000, quic=":443"; ma=2592000; v="43,46"
{"test_list":[{"id":"19","name":"BLOOD Grouping","image":"https://dakshalab.com/dakshalab/admin/assets/img/19.png"},{"id":"11","name":"BLOOD Grouping&RH Factor","image":"https://dakshalab.com/dakshalab/admin/assets/img/11.png"},{"id":"18","name":"Blood Sugar","image":"https://dakshalab.com/dakshalab/admin/assets/img/18.png"},{"id":"3","name":"CCP","image":"https://dakshalab.com/dakshalab/admin/assets/img/3.png"},{"id":"17","name":"CCP-Ab to Cyclic Citrullinated Peptide, Serum by CLIA","image":"https://dakshalab.com/dakshalab/admin/assets/img/17.png"},{"id":"24","name":"HBA1C","image":"https://dakshalab.com/dakshalab/admin/assets/img/24.png"},{"id":"10","name":"Hemoglobin ","image":"https://dakshalab.com/dakshalab/admin/assets/img/10.png"},{"id":"20","name":"Rh Factor","image":"https://dakshalab.com/dakshalab/admin/assets/img/20.png"},{"id":"1","name":"Sr. TOTAL CHOLESTEROL (CHOD POD)","image":"https://dakshalab.com/dakshalab/admin/assets/img/1.png"},{"id":"2","name":"Sr. TRIGLYCERIDES (Lipase-Glycerol Kinase)","image":"https://dakshalab.com/dakshalab/admin/assets/img/2.png"},{"id":"13","name":"Test11","image":"https://dakshalab.com/dakshalab/admin/assets/img/13.png"},{"id":"5","name":"Test2","image":"https://dakshalab.com/dakshalab/admin/assets/img/5.png"},{"id":"14","name":"Test22","image":"https://dakshalab.com/dakshalab/admin/assets/img/14.png"},{"id":"6","name":"Test3","image":"https://dakshalab.com/dakshalab/admin/assets/img/6.png"},{"id":"15","name":"Test33","image":"https://dakshalab.com/dakshalab/admin/assets/img/15.png"},{"id":"7","name":"Test4","image":"https://dakshalab.com/dakshalab/admin/assets/img/7.png"},{"id":"16","name":"Test44","image":"https://dakshalab.com/dakshalab/admin/assets/img/16.png"},{"id":"8","name":"Test5","image":"https://dakshalab.com/dakshalab/admin/assets/img/8.png"},{"id":"9","name":"Test6","image":"https://dakshalab.com/dakshalab/admin/assets/img/9.png"},{"id":"21","name":"TYPHI DOT (IGM)","image":"https://dakshalab.com/dakshalab/admin/assets/img/21.png"},{"id":"23","name":"Vitamin C","image":"https://dakshalab.com/dakshalab/admin/assets/img/23.png"},{"id":"22","name":"Vitamin D","image":"https://dakshalab.com/dakshalab/admin/assets/img/22.png"}]}
It shows that the header access-control-allow-origin: * is coming from the server. But even after that in the browser console I am getting the same error message. What could be the reason behind it?