随笔 - 91  文章 - 0  评论 - 2  阅读 - 44255

jsonp跨站请求

本地端

url:

url(r'req/',a2.req),

 

commons.js

alert(123);

 

views.py

from django.shortcuts import render
import requests
# Create your views here.
def req(request):
    response=requests.get('http://www.weather.com.cn/data/cityinfo/101010100.html')
    print(response.content) #字节类型
    response.encoding='utf-8'
    print(response.text)  #字符串类型
    print(response.cookies,response.headers,)
    return render(request,'req.html',{'result':response.text})

 

本地前端

req.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
{#    <script src="/static/commons.js"></script>#}
{#    <script src='http://127.0.0.1:8001/jsonp/?k1=v1&k2=v2'></script>#}
{#    <script src="https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>#}
</head>
<body>
    <h1>后台获取的结果</h1>
        {{ result }}
    <h1>js直接获取结果</h1>
    <input type="button" value="获取数据" onclick="getContent();">
    <div id="container_1"></div>
        <script src="/static/jquery-1.12.4.js"></script>
        <script>
            function getContent() {
                 /*
                var xhr=new XMLHttpRequest();

                {#                xhr.open("GET",'http://www.weather.com.cn/data/cityinfo/101010100.html')#}

                xhr.open("GET",'http://127.0.0.1:8001/jsonp.html?k1=v1&k2=v2');
                xhr.onreadystatechange=function () {
                  console.log(xhr.responseText);//拿到返回的文本信息
                };
                xhr.send()
                */
                /*
                var tag=document.createElement('script');
{#                tag.src='http://127.0.0.1:8001/jsonp/?callback=pe&k1=v1&k2=v2';#}

                tag.src='http://www.jxntv.cn/data/jmd-jxtv2.html?callback=list&_=1454376870403';
                document.head.appendChild(tag);
                document.head.removeChild(tag);
            }
            function list(arg) {
                console.log(arg);
            }
             */
            $.ajax({
                url:'http://www.jxntv.cn/data/jmd-jxtv2.html?_=1454376870403',
                type:'POST',
                dataType: 'jsonp',
                jsonp: 'callback',
                jsonpCallback: 'list'
            });
            }
            function list(arg) {
                console.log(arg);
            }
        </script>
</body>
</html>

 

远程端

url

url(r'jsonp/',views.jsonp),

 

views.py

def jsonp(request):
    func=request.GET.get('callback')
    content='%s(1000)'%(func,)
    return HttpResponse(content)

    # print(request.GET)
    # return HttpResponse('alert("21")')

 

笔记:

由于浏览器具有同源策略(阻止ajax请求,无法阻止<script src='---'></script>)
要取外面网站数据怎么办:


--创建script标签
--src=远程地址
--远程地址返回的内容能拿到 必须符合javacript格式

以?号浏览器url获取是get方式获取数据


jsonp只能发get请求

 

posted on   SZ_文彬  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示