zoukankan      html  css  js  c++  java
  • 【linux】nginx options 跨域问题 请求HTTP错误405 用于访问该页的HTTP动作未被许可 Method Not Allowed

    JavaScript JS 跨域问题

    HTTP 错误 405 - 用于访问该页的 HTTP 动作未被许可
    HTTP 错误 405.0 - Method Not Allowed

    Nginx 处理跨域问题、OPTIONS 方法的问题

    Method = "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | extension-method
    extension-method = token

    解决办法:


    在Nginx location 里加上如下代码可以解决js 请求跨域问题:

     location / {
            if (!-e $request_filename){
                rewrite  ^(.*)$  /index.php?s=$1  last;   break;
            }
            
            
            if ($request_method = 'OPTIONS') { 
              add_header Access-Control-Allow-Origin *; 
              add_header Access-Control-Allow-Credentials true; 
              add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; 
              add_header '*'; 
              return 200; 
            }
    
    
            if ($request_method = 'POST') {
              add_header 'Access-Control-Allow-Origin' *; 
              add_header 'Access-Control-Allow-Credentials' 'true'; 
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
              add_header '*';
            }
    
    
            if ($request_method = 'GET') {
              add_header 'Access-Control-Allow-Origin' *; 
              add_header 'Access-Control-Allow-Credentials' 'true'; 
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
              add_header '*';
            }

     }

    
    

    注意,必须放在 location ... { ... }里面才能用if条件判断。

  • 相关阅读:
    Python的正则表达式
    Python的异常处理
    Python的类和对象
    Python乘法口诀表
    Python的文件操作
    三层架构介绍和MVC设计模型介绍
    spring的组件使用
    IDEA使用maven搭建spring项目
    Java集合——Collection接口
    Java集合——概述
  • 原文地址:https://www.cnblogs.com/richerdyoung/p/9272445.html
Copyright © 2011-2022 走看看