api接口开发跨域注意事项和设置

由于跨域请求会先发送一个OPTIONS请求,因此须要判断下OPTIONS请求的就直接返回json

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');
相关文章
相关标签/搜索