因为跨域请求会先发送一个OPTIONS请求,所以需要判断下OPTIONS请求的就直接返回

if(strtoupper($_SERVER['REQUEST_METHOD'])== 'OPTIONS'){
      header("Access-Control-Allow-Origin:*");
      header("Access-Control-Allow-Headers:content-type");
      header("Access-Control-Request-Method:GET,POST");
      exit;
}

在返回json数据的时候需要在头部加上

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST');
header('Access-Control-Allow-Headers: Destination, Content-Type, User-Agent, X-Requested-With, If-Modified-Since');
header('Access-Control-Allow-Credentials: true');

 

相关文章:

  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-09-22
  • 2022-12-23
  • 2022-01-08
  • 2021-05-26
猜你喜欢
  • 2021-05-14
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
  • 2021-04-14
相关资源
相似解决方案