Add header Access-Control-Allow-Origin to XMLHttpResponse in php

*爱你&永不变心* 提交于 2019-12-13 03:59:42

问题


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?


回答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)




回答2:


You can put it like below:

header('Access-Control-Allow-Origin: *');


来源:https://stackoverflow.com/questions/14496874/add-header-access-control-allow-origin-to-xmlhttpresponse-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!