zoukankan      html  css  js  c++  java
  • CORS跨域请求

    CORS即Cross Origin Resource Sharing 跨域资源共享,

    那么跨域请求还分为两种,一种叫简单请求,一种是复杂请求~~

    简单请求:

          HTTP方法是下列方法之一

      HEAD, GET,POST

    HTTP头信息不超出以下几种字段

      Accept, Accept-Language, Content-Language, Last-Event-ID

      Content-Type只能是下列类型中的一个

        application/x-www-from-urlencoded

        multipart/form-data

        text/plain

    任何一个不满足上述要求的请求,即会被认为是复杂请求~~

    复杂请求会先发出一个预请求,我们也叫预检,OPTIONS请求~~

    同源策略:

    跨域是因为浏览器的同源策略导致的,也就是说浏览器会阻止非同源的请求~

    那什么是非同源呢~~即域名不同,端口不同都属于非同源的~~~

    浏览器只阻止表单以及ajax请求,并不会阻止src请求,所以我们的cnd,图片等src请求都可以发~~

    解决方式:

     1 from django.utils.deprecation import MiddlewareMixin
     2 
     3 
     4 class MyCore(MiddlewareMixin):
     5     def process_response(self, request, response):
     6         response["Access-Control-Allow-Origin"] = "*"
     7         if request.method == "OPTIONS":
     8             response["Access-Control-Allow-Headers"] = "Content-Type"
     9             response["Access-Control-Allow-Methods"] = "DELETE, PUT, POST"
    10         return response
  • 相关阅读:
    hdu1593(find a way to escape)
    每日学习小记 11/02
    将博客搬至CSDN
    浏览器渲染机制
    适配器模式 The Adapter Pattern
    工厂方法模式 The Factory Method Pattern
    观察者模式 The Observer Pattern
    模板方法模式 The Template Method Pattern
    命令模式 The Command Pattern
    迭代器模式 The Iterator Pattern
  • 原文地址:https://www.cnblogs.com/duanhaoxin/p/9911308.html
Copyright © 2011-2022 走看看