我使用了两个域名: - localhost: 3000 (reactjs) - Localhost (php) 出现的问题:当我在php服务器上用react js发送数据时。每个 session_id (php) 请求都会发生变化,所以我无法将信息保留在 sesison (php) 中。
我的 php 代码(标题):
<?php
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, POST, DELETE, PUT, PATCH, OPTIONS');
header('Access-Control-Allow-Headers: token, Content-Type');
header('Access-Control-Max-Age: 1728000');
header('Content-Length: 0');
header('Content-Type: text/plain');
header('Access-Control-Allow-Credentials: true');
die();
}
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');?>
我的反应代码
axios.post(
'http://localhost/Corro2/connection.php',
JSON.stringify(this.state.values),
{
withCredentials: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
}
}).then(res => res.json())
.then(data => {
this.setState({
error:''
})
for(let x in data)
{
for(let y in data[x])
{
this.setState({
error: data[x][y]
})
}
}
}).catch(err => console.error("Error:", err)); }