fetch('http://example.com/api/user/123', { method: 'GET', headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
$user_id = $_GET['user_id']; // 检查参数是否正确
// 连接数据库、执行查询等操作
$response = array( 'success' => true, 'data' => array( 'user_id' => $user_id, // 其他数据 ) );
echo json_encode($response); ?>
// PHP 响应头 header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); header('Access-Control-Allow-Headers: Content-Type');
// fetch 请求代码 fetch('http://example.com/api/user/123', { method: 'GET', credentials: 'include', // 允许携带 cookie 等凭据跨域 headers: { 'Content-Type': 'application