加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsonp跨站请求

发布时间:2020-12-16 18:40:05 所属栏目:百科 来源:网络整理
导读:本地端 url: url( r‘req/‘ ,a2.req), ? 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‘ ) ????pri

本地端

url:

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

?

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")‘)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读