分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

JSONP

发布时间:2023-09-06 01:54责任编辑:郭大石关键词:JSONP
def jsonp(request): ???#JSONP存在的原因是浏览器具有同源(域名)策略 (浏览器阻止Ajax,但是无法阻止<script src=‘..‘></script>) ???#奇招: ????????#创建script标签 ????????#src=远程地址 ????????#返回的数据必须是js格式 ????????#jsonp只能发GET请求 ???response=requests.get(‘http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101121301‘) ???print(response.content) ???response.encoding=‘utf-8‘ ???return render(request,‘jsonp.html‘,{‘result‘:response.text})def jsonpp(request): ???print(request.GET) ???ret =‘‘‘callback(123)‘‘‘ ???return HttpResponse(ret)
Views.py
<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>Title</title></head><body><h1>后台获取结果</h1>{{ result }}<h1>js直接获取结果</h1><input type="button" value="获取数据" onclick="getContent();"><div id="content"></div><script src="/static/jquery-1.12.4.js"></script><script> ???function getContent() { ???????{# ???var xhr = new XMLHttpRequest();#} ???????{# ???xhr.open(‘GET‘,‘http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101121301‘);#} ???????{# ???xhr.onreadystatechange=function(){#} ???????{# ???????console.log(xhr.responseText);#} ???????{# ???}#} ???????{# ???xhr.send();#} ???????{# ???????var tag=document.createElement(‘script‘);#} ???????{# ???????tag.src=‘http://127.0.0.1/jsonpp/?k1=v1&k2=v2‘;#} ???????{# ???????document.head.appendChild(tag);#} ???????{# ???????document.head.remove(tag);#} ???????{##} ???????{##} ???????{# ???}#} ???????{##} ???????//ajax方式 ???????$.ajax({ ???????????url:‘http://www.jxntv.cn/data/jmd-jxtv2.html‘, ???????????type:‘POST‘, ???????????dataType:‘jsonp‘, ???????????jsonp:‘callback‘, ???????????jsonCallback:‘list‘ ???????}) ???} ??????????function list(arg) { ???????????????console.log(arg); ???????????}</script></body></html>
html

JSONP

原文地址:https://www.cnblogs.com/sunhao96/p/9040982.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved