• 跨域


    1.简单跨域

    http://127.0.0.1:8001/http://127.0.0.1:8002/获取数据

    http://127.0.0.1:8001/

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
     7 </head>
     8 <body>
     9 <h2>s1页面</h2>
    10 <button id="get">获取s2的数据</button>
    11 <script>
    12     $("#get").click(function () {
    13         $.ajax({
    14             url:"http://127.0.0.1:8002/sendmsg/",
    15             success:function (ret) {
    16                 console.log(ret)
    17             }
    18         })
    19     })
    20 </script>
    21 </body>
    22 </html>

    http://127.0.0.1:8002/

    1 from django.shortcuts import render,HttpResponse
    2 def sendmsg(request):
    3     list=["amy","lucy","amily"]
    4     import json
    5     ret=HttpResponse(json.dumps(list))
    6     #核心代码设置请求头中的Access-Control-Allow-Origin为*(所有)或http://127.0.0.1:8001/
    7     ret['Access-Control-Allow-Origin']="*"
    8     return ret

    2.复杂跨域

    http://127.0.0.1:8001/http://127.0.0.1:8002/获发送json数据数据

    http://127.0.0.1:8001/

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
     7 </head>
     8 <body>
     9 <h2>s1页面</h2>
    10 <button id="get">获取s2的数据(复杂)</button>
    11 <button id="easy">获取s2的数据(简单)</button>
    12 <script>
    13     $("#get").click(function () {
    14         $.ajax({
    15             url:"http://127.0.0.1:8002/sendmsg/",
    16             type:"post",
    17             contentType:"json",
    18             data:JSON.stringify(["aaa","bbb"]),
    19             success:function (ret) {
    20                 console.log(ret)
    21             }
    22         })
    23     })
    24     $("#easy").click(function () {
    25         $.ajax({
    26             url:"http://127.0.0.1:8002/sendmsg/",
    27             success:function (ret) {
    28                 console.log(ret)
    29             }
    30         })
    31     })
    32 </script>
    33 </body>
    34 </html>

    http://127.0.0.1:8002/

     1 from django.shortcuts import render,HttpResponse
     2 def sendmsg(request):
     3     list=["amy","lucy","amily"]
     4     import json
     5     ret=HttpResponse(json.dumps(list))
     6     ret['Access-Control-Allow-Origin']="*"
     7     #核心代码
     8     ret["Access-Control-Allow-Headers"]="Content-Type"
     9     if request.method=="POST":
    10         #前端传递过来的json数据
    11         print(request.body)
    12     return ret

     

     

     

  • 相关阅读:
    redis命令
    linux命令行任务管理
    tomcat修改内存
    Python调用shell
    取消myeclipse自动进入workspace
    解决Myeclipse编译不生成.class文件问题
    Manacher回文串算法学习记录
    青少年如何使用 Python 开始游戏开发
    对 Linux 专家非常有用的 20 个命令
    对中级 Linux 用户非常有用的 20 个命令
  • 原文地址:https://www.cnblogs.com/shanghongyun/p/10062425.html
Copyright © 2020-2023  润新知