跨域请求

1.跨域请求

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

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

1.1简单请求

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请求~~
demo.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
    <script>
        function handlerResponse(data) {
            console.log(data)
        }
    </script>
{#    <script src="http://127.0.0.1:8000/demo/"></script>#}
</head>
<body>
<div id="app">

</div>
<script>
    const app = new Vue({
        el: "#app",
        mounted(){
            axios.request({
                url: "http://127.0.0.1:8000/demo/",
                method: "PUT"

            }).then(function (data) {
                console.log("data----------",data)
            })
        }
    })
</script>

</body>
</html>
views.py

from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.response import Response

# Create your views here.

class DemoView(APIView):
    def get(self, request):
        res = "handlerResponse('跨域测试')"
        # res = "ce测试"
        return HttpResponse(res)

    def put(self, request):
        return Response("put接口测试")

    def post(self, request):
        return Response("POST接口测试")

跨域请求

1.2浏览器的同源策略

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

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

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

1.3jsonp解决跨域

class DemoView(APIView):
    def get(self, request):
        #前端书写了handlerResponse函数,来处理数据
        res = "handlerResponse('跨域测试')"
        # res = "ce测试"
        return HttpResponse(res)

跨域请求

1.4中间件解决跨域

上面的方式知道即可,不怎么使用了
settings.py

MIDDLEWARE = [
    'untitled.middlewares.MyCors',
]
middlewares.py

from django.middleware.security import SecurityMiddleware
from django.utils.deprecation import MiddlewareMixin

class MyCors(MiddlewareMixin):

    def process_response(self, request, response):
        # 哪些域不拦截
        response["Access-Control-Allow-Origin"] = "*"
        if request.method == "OPTIONS":
            response["Access-Control-Allow-Methods"] = "PUT, DELETE"
            response["Access-Control-Allow-Headers"] = "content-type"
        return response
views.py

from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.response import Response

# Create your views here.

class DemoView(APIView):
    def get(self, request):
        res = "handlerResponse('跨域测试')"
        # res = "ce测试"
        return HttpResponse(res)

    def put(self, request):
        return Response("put接口测试")

    def post(self, request):
        return Response("POST接口测试")
demo.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
    <script>

    </script>

</head>
<body>
<div id="app">

</div>
<script>
const app = new Vue({
        el: "#app",
        mounted(){
            axios.request({
                url: "http://127.0.0.1:8000/demo/",
                method: "POST",
                data: {
                    "name": "Alex"
                }
            }).then(function (data) {
                console.log(data)
            })
        }
    })
</script>

</body>
</html>

跨域请求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值