php - ci 休息服务器允许 CORS 不起作用
问题描述
嘿伙计们,我有大学的任务来制作电子学习休息服务器,当我将它部署到 VPS 时,它收到一个错误消息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://IP_VPS/elearning/auth/login. (Reason: CORS request did not succeed).
我正在使用 chriskacerguis ci 库,控制器是AUTH.php延伸到Token.php延伸到REST_Controller.php。
应用程序/控制器/api/Token.php
<?php
// load library rest server
use Restserver\Libraries\REST_Controller;
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
// load library rest server
use \Firebase\JWT\JWT;
require APPPATH . 'libraries/JWT.php';
require APPPATH . 'libraries/BeforeValidException.php';
require APPPATH . 'libraries/ExpiredException.php';
require APPPATH . 'libraries/SignatureInvalidException.php';
defined('BASEPATH') OR exit('No direct script access allowed');
class Token extends REST_Controller {
private $_secretkey = 'elearning';
private $_token;
public function __construct(){
// ALLOW CORS
header('Access-Control-Allow-Origin: *');
if($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
exit;
}
parent::__construct();
$this->load->model('m_users', 'user');
}
public function getToken() {
return $this->_token;
}
public function generateToken($user){
}
public function authToken($admin_access = null){
}
public function decodeToken($token) {
}
}
?>
在application/controllers/Auth.php 中,我没有将标头 CORS 放入构造函数,因为我之前已经将其放入,但仍然无法正常工作。
客户端(我将其上传到 github.io)
let xhr = new XMLHttpRequest();
xhr.open('POST', 'https://IP_VPS/elearning/auth/login');
xhr.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
console.log(this.response);
}
}
xhr.send('email='+email.value+'&password='+password.value);
/etc/nginx/sites-available/default
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Con$
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Con$
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Con$
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
# codeigniter elearning
# i created it by myself
location /elearning {
try_files $uri $uri/ /elearning/index.php;
}
我的控制器/配置有什么问题吗?我在构建休息服务器并部署它时超级新。对不起,如果这个问题太愚蠢了,我真的很困惑我现在应该做什么。我希望我的问题不会被关闭。还是谢谢各位
解决方案
推荐阅读
- php - PHP不与arraypart比较
- css - SVG 缩放 (100% - 60px)
- c# - 在每个非字母/非数字字符处拆分字符串
- reactjs - Webpack:414 Request-URI Too Long for an image
- php - 如何修复来自 php 页面的评论在页面重新加载时重复发布?
- node.js - 节点 10 无法安装 package.json 中列出的依赖项
- delphi - 如何在 Delphi 中将 TMemo 用于电子邮件正文
- azure-data-factory - 在 Azure 数据工厂中将行数转换为 INT
- .net - .NET Core 3.1 - SQL Server - 创建新的登录名和用户 - 参数问题
- java - Hibernate 可以将子选择中的 1:M 连接的结果集映射到具有子集合的父级吗?