在前端里面,解决跨域的时候总显得那么的恶心,什么jsonp啊,ajax啊,CORS啊什么的,总觉得是在钻空子进行跨域,有两种办法
第一种办法:在代码内配置
其实在PHP文件里面只需要加一段代码就可以跨域了,前端你该怎么写还是怎么写,post,get随便用:
header("Access-Control-Allow-Origin=*");//允许全部域名访问
或者
header("Access-Control-Allow-Origin:www.baidu.com")//限制一个域名访问
header("Access-Control-Allow-Methods:POST");//限制POST请求
header("Access-Control-Allow-Headers:x-requested-with,content-type");
![6c1e3951628f811db1bdc3bf696ebfae.png](https://img-blog.csdnimg.cn/img_convert/6c1e3951628f811db1bdc3bf696ebfae.png)
第二种办法:在NGINX内配置
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers "x-requested-with,Authorization";
add_header Access-Control-Allow-Methods *;
![dd46afac29a4a47a90b699855c327643.png](https://img-blog.csdnimg.cn/img_convert/dd46afac29a4a47a90b699855c327643.png)