无法接收带有axios发布请求的Set-Cookie标头 [英] Not Receiving Set-Cookie Header with axios post request

查看:322
本文介绍了无法接收带有axios发布请求的Set-Cookie标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,如果直接在浏览器中(或由邮递员)调用,则可以成功返回一些简单的标头以及set-cookie标头.我可以从chrome devTools中读取类似的响应标题.但是,一旦我由Axios调用它,set-cookie标头就不会显示,并且浏览器中也没有保存cookie.

I have a PHP Script which successfully returns some simple Headers as well as a set-cookie header if called directly in the browser (or by postman). I can read the response-headers like that from chrome devTools. But as soon as I call it by Axios, the set-cookie header doesn't show up and there's no cookie saved in the browser.

我尝试了各种不同的操作,例如更改服务器端的响应头以及在axios上使用"withCredentials:true",但没有任何效果.我什至没有收到错误或任何与cors相关的问题.

I tried diffrent things like changing the response-headers server-side and using "withCredentials: true" with axios, but nothing worked. I don't even get an error or any cors-related problems.

PHP:

header("Access-Control-Allow-Origin: http://localhost:8080");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
header("Access-Control-Max-Age: 99999999");
setcookie("TestCookie", "Testing", time() + 3600, "/", "localhost", 0);
die();

JS:

Vue.prototype.$http = axios.create({
    baseURL: XYZ,
    withCredentials: true
})

所以我的第一个问题是,为什么直接调用php脚本时标题会出现?以及如何通过axios归档以获取标头?

So my first question is why does the header appear when calling the php script directly? And how can I archive to get the header through axios too?

推荐答案

cookie可能是"httpOnly",这意味着客户端javascript无法读取它. 因此,它不会在Chrome Cookie部分中显示. 在mozilla中测试相同的请求,标题将会显示.

probably cookie is 'httpOnly', which means client side javascript can not read it. Therefore it is not showing in chrome cookie section. test the same request in mozilla, header will show up.

这篇关于无法接收带有axios发布请求的Set-Cookie标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆