zoukankan      html  css  js  c++  java
  • Nginx 实现AJAX跨域请求

    在工作中遇到跨域请求的问题:

    AJAX从一个域请求另一个域会有跨域的问题。那么如何在nginx上实现ajax跨域请求呢?要在nginx上启用跨域请求,需要添加add_header Access-Control*指令。如下所示:


               if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' 'http://192.168.220.173';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            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' 'http://192.168.220.173';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
             }
             if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' 'http://192.168.220.173';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Metshods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
             }

    第一条指令:授权从192.168.220.173的请求

    第二条指令:当该标志为真时,响应于该请求是否可以被暴露

    第三天指令:指定请求的方法,可以是GET,POST等

  • 相关阅读:
    VS2010 MFC对话框程序用CButtonST给按钮添加图标
    VS2010 MFC 使用GDI+给图片添加汉字
    C++ Primer(第4版)-学习笔记-第2部分:容器和算法
    C++ 面向对象编程
    C++类(Class)总结
    delegate、notification、KVO场景差别
    iOS block种类和切换
    Copy 与MutableCopy的区别
    ios 避免循环引用
    WKInterfaceImage 无法更新图片的问题
  • 原文地址:https://www.cnblogs.com/smail-bao/p/5411141.html
Copyright © 2011-2022 走看看