闻心阁

一蓑烟雨看苍生,半壶浊酒笑红尘

PHP Cross-domain Setting

2016-08-07 约 1 分钟读完 搬砖秘籍

In the previous article 《jQuery $.ajax Method Ignored the Undefined Values》, I send a request to the server’s php file, maybe you would get a cross-domain error.

XMLHttpRequest cannot load http://127.0.0.1/jq-undefined.php?para1=1&para2=2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Because in the default setting, the brower is believe your request is Illegal and unsafe, so your request would be rejected.

Solution

You can change the default setting in you php files, there is three basic setting.

header('Access-Control-Allow-Origin:*');  
header('Access-Control-Allow-Methods:POST');  //set the allow method
header('Access-Control-Allow-Headers:x-requested-with,content-type');  

In common condition, you just need the first one.