How to set headers for a file on a server ? | Sololearn: Learn to code for FREE!

+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.

4/14/2018 4:07:17 AM

Swapnil Srivastava

5 Answers

New Answer

+15

I found the error. There didn't have to be any space between Origin and colon. Anyways, thanks for your interest everyone.

+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.

+2

Have you tried specifying the *dataType* as *jsonp* while requesting the page via ajax.

+2

https://code.sololearn.com/W6mtn3foR4a7/?ref=app

+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