[英]PHP-Session in API changes when called by Axios in React
我正在使用PHP編寫Api。 Api由React Web客戶端調用。 我正在使用Axio來調用Api。 當我向Api發送內容時,SessionID不是持久性的,它在每次調用后都會更改。 有人有什么主意嗎?
簡化的PHP-Api:
<?php
session_start();
echo session_id();
/*
//Doesnt mather
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header('Content-type: application/json; charset=utf-8');
*/
?>
簡化的React-Client
static vote(vote, callback)
{
const data = {
action: 'vote',
vote: vote,
}
axios.post(api_url, qs.stringify(data))
.then(callback)
.catch((error) => {
console.log(error);
});
}
我不確定這是否重復或只是一個非常相關的問題: 使Axios自動在其請求中發送cookie
我猜您的api和您的客戶端在不同的域上運行。 為了使瀏覽器在這種情況下存儲cookie,您需要{ withCredentials: true }
作為配置:
axios.post(api_url, qs.stringify(data), { withCredentials: true })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.