https协议下,post请求跨域,get请求没跨域;

https协议下,post请求跨域,get请求没跨域,http协议没问题;

相关代码

clipboard.png

clipboard.png

clipboard.png

clipboard.png

clipboard.png

阅读 13.9k
3 个回答

PHP 代码:

header('Access-Control-Allow-Origin:' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods:GET,POST,OPTIONS,PUT');
header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization');

JS请求代码:

$.ajax({
   type: "POST",
   xhrFields:{
              withCredentials:true
            },
   success: function(){
     
   }
});

还是跨域的问题没有解决。

前端需要允许跨域,加入xhrFields,jQuery的话,大概这样:

$.ajax({
   type: "POST",
   xhrFields:{
              withCredentials:true
            },
   success: function(){
     
   }
});

后端加入header

header("Access-Control-Allow-Credentials", "true"); 
header("Access-Control-Allow-Origin", "允许跨域的地址,如果全部允许写*"); 

// 使用通配符 * ,表示当前服务端 返回的信息允许所有源访问,也可指定可信任的域名来接收响应信息


header("Access-Control-Allow-Origin: http://localhost:3000");

header("Access-control-Allow-Origin:*");

// 响应头设置为ajax提交


header("Access-Control-Allow-Headers:X-Requested-With");

// 允许携带 用户认证凭据(也就是允许客户端发送的请求携带Cookie)


header("Access-Control-Allow-Credentials:true");

写到方法内部

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题