How to set headers for a file on a server ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

How to set headers for a file on a server ?

I was trying to call a page I hosted on 000webhost with ajax but it gave error that the Access-Control-Allow-Origin header was missing. My question is to how to set this header ? I have no experience in backend and have no idea what headers are. I tried putting Header set Access-Control-Allow-Origin "*" in the .htaccess file and putting header("Access-Control-Allow-Origin : *") in the php file I was trying to call but in vain.

14th Apr 2018, 4:07 AM
Swapnil Srivastava
Swapnil Srivastava - avatar
5 Answers
+ 15
I found the error. There didn't have to be any space between Origin and colon. Anyways, thanks for your interest everyone.
14th Apr 2018, 4:54 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 17
Curly Brace thanks for your answer. However, I've already fixed the problem. There was just an additional space between Origin and the colon in the string that was passed to the header function which was causing the problem.
15th Apr 2018, 2:26 AM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 2
Have you tried specifying the *dataType* as *jsonp* while requesting the page via ajax.
14th Apr 2018, 3:50 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar
14th Apr 2018, 3:51 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar
+ 2
may be your server code outputs something before you actually get your headers set? for example in php it could be just a BOM code (which is visible only in some text editors tho like notepad++) at the php file beginning
14th Apr 2018, 5:27 PM
Curly Brace
Curly Brace - avatar