0

I am sending XHR request to server written in php which is working fine. but at some cross domain origin are not allowing loading and processing the response from server. it is throwing error

XMLHttpRequest cannot load http://mydomain/getInfo?blabla. Origin http://www.somedomain.com is not allowed by Access-Control-Allow-Origin.

so how can I set Access-Control-Allow-Origin in the response headers in php?

Mahendra
  • 153
  • 2
  • 5
  • 15

2 Answers2

4

You can put it like below:

header('Access-Control-Allow-Origin: *');
Minesh
  • 2,284
  • 1
  • 14
  • 22
  • thanks for reply.. I am quit new to PHP so I want to know where I can put it in code my code which responses to request $this->response($this->json($res), 200); – Mahendra Jan 24 '13 at 08:45
  • welcome @Mahendra You may want to select this answer as solution. – Minesh Jan 24 '13 at 08:51
  • @Mahendra where did u add the header?? i am also trying to figure out. did u add it in ur html page? – ueg1990 Feb 10 '14 at 18:15
1

If your JavaScript runs on example.com and the server you are requesting to is in example.net. Make sure it allows the page your js is running by this header

Access-Control-Allow-Origin: http://example.com/ajax.html

Also you need to send Origin header from ajax to the server

Origin: http://example.com/ajax.html

More information can be found on HTTP access control (CORS)

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187