zoukankan      html  css  js  c++  java
  • 跨域Ajax -- jsonp和cors

    跨域Ajax
    - jsonp
    - cors
    
    参考博客:
    http://www.cnblogs.com/wupeiqi/articles/5703697.html
    http://www.cnblogs.com/wupeiqi/articles/5369773.html
    
    JSONP
    网站:
    - requests发请求时,跨域无限制
    - ajax发请求时,浏览器限制【是否可以绕过限制?】
    
    如何绕过浏览器的同源策略?
    - ajax,受同源策略限制。
    - img,script,iframe,不受同源策略限制。
    
    - JSONP:
    	本地:先定义函数
    	远程:func("数据")
    	
    	方式一:手动
    			/*
    			function funcvvvvvv(arg) {
    				alert(arg);
    
    				document.head.removeChild(tag);
    			}
    
    			function jsonp(url){
    				tag = document.createElement('script');
    				tag.src = url;
    				document.head.appendChild(tag);
    			}*/
    	方式二:调用jQuery
    			function Jsonp2(){
    				$.ajax({
    					url: "http://127.0.0.1:8000/get_data.html",
    					type: 'GET',
    					dataType: 'JSONP',
    					success: function(data){
    						console.log(data);
    					}
    				})
    			}
    	本质:创建script标签	
    
    	编写跨域请求:
    		本地:
    			
    			function list666(arg){
    			
    			}
    			
    			$.ajax({
    				url: "http://www.jxntv.cn/data/jmd-jxtv2.html",
    				type: 'GET',
    				dataType: 'JSONP',
    				jsonp: 'callback',
    				jsonpCallback: 'list666'
    			})
    			
    		远程:
    			func_name = request.GET.get('callback')
    			return HttpResponse('%s("机密数据")' %func_name)
    			
    	应用场景:
    		 你,调用者
    		CEO,服务商,提供API
    		
    		- JSONP交互
    		- requests模块
    		
    		
    		用户,调用者
    		  你,服务商,提供API
    		  
    		  专业:获取用户请求callback,func(内容)
    	
    	JSONP是否可以发送POST请求?

    CORS
    本地:无作为
    远程:设置响应头
    response['Access-Control-Allow-Origin'] = "http://127.0.0.1:8888"
    response['Access-Control-Allow-Methods'] = "PUT"
    response['Access-Control-Allow-Headers'] = "xxx"

      

  • 相关阅读:
    poj 2763 Housewife Wind
    hdu 3966 Aragorn's Story
    poj 1655 Balancing Act 求树的重心
    有上下界的网络流问题
    URAL 1277 Cops and Thieves 最小割 无向图点带权点连通度
    ZOJ 2532 Internship 网络流求关键边
    ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路
    SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流
    怎么样仿写已知网址的网页?
    5-10 公路村村通 (30分)
  • 原文地址:https://www.cnblogs.com/linzetong/p/8849828.html
Copyright © 2011-2022 走看看