《PHP-ThinkPHP-前后分离跨域》

tp5.1 前后端分离api开发时ajax跨域请求中请求头header需要注意的问题

前后端分离开发过程中,使用ajax做请求的时候需要注意几个问题

1.预请求OPTIONS问题

 请求头代码改为

if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
        header('Access-Control-Allow-Origin:http://runapi.showdoc.cc');
        header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,Token');
        header('Access-Control-Allow-Credentials:true');
        header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
        header('Access-Control-Max-Age:1728000');
        header('Content-Type:text/plain charset=UTF-8');
        header('Content-Length: 0', true);
        header('status: 200');
        header('HTTP/1.0 204 No Content');
        exit;
    }else{
        header('Access-Control-Allow-Origin:http://runapi.showdoc.cc');
        header('Access-Control-Allow-Credentials: true');
        header("Access-Control-Allow-Methods:GET, POST, PUT,DELETE,POSTIONS");
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie,Token");
    }

2.请求来源域名跨域问题

  不指定请求来源地址参数为*   header('Access-Control-Allow-Origin:*');
  指定了请求来源地址的话,参数为地址域名  例如:  header('Access-Control-Allow-Origin:http://www.xxx.com');

参考链接:https://www.freesion.com/article/99691422047/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小霸王_30037863

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值