PHP Cross-domain Setting

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.

 标签: PHP, Cross-domain

作者  :  sailor

仅仅看明白了还不够,能把别人给讲明白了才算及格。



最新文章