• CORS实践


     $.ajax("http://yafbox.18touch.com/", {  
    			  type: "POST",
    			  data:	{id:id,v:v},	
    			  //headers:	{"Liu":"M1"},	
    			  dataType:"json",
    			  xhrFields: {  
    				withCredentials: true 
    			  },  
    			  crossDomain: true,  
    			  success: function(data, status, xhr) {  
    				console.log(data);
    				console.log(status);
    				console.log(xhr);
    				console.log(xhr.getAllResponseHeaders());
    				console.log(xhr.getResponseHeader("liu"));
    			  }  
    			}); 
    

     跨域必须有crossDomain: true,带cookie需要xhrFields: { withCredentials: true }

    headers:	{"Liu":"MENG1"},	测试不成功,解析不了

    console.log(xhr.getAllResponseHeaders());
    console.log(xhr.getResponseHeader("liu"));
    不跨域可以获取,但是跨域只能获取content-type头

    服务端:
    header("Access-Control-Allow-Origin: http://mh.18touch.com");
    		//header("Access-Control-Allow-Origin: *");
              header("Access-Control-Allow-Methods: GET,POST,PUT"); header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Headers","Content-Type,liu"); header("Access-Control-Expose-Headers: true"); header("liu: M"); echo '{"id":'.$_POST['id'].',"v":'.$_POST['v'].',"ak":"'.$_COOKIE['ak'].',"}'; setcookie ( "Testzy" , "liumengzy" , time ()+ 3600 , "/" , ".18touch.com" , 1 ); setcookie ( "Testbox" , "liumengzbox" , time ()+ 3600 , "/" , "yafbox.18touch.com" , 1 ); exit;

     设置header("Access-Control-Allow-Origin: *");jq的ajax请求跨预料域,但是post方法可以。。。

    设置完整的域名都可以,跨域读写cookie都可行



  • 相关阅读:
    python经典算法面试题1.5:如何找出单链表中的倒数第K个元素
    python经典面试算法题1.4:如何对链表进行重新排序
    巧妙利用引用,将数组转换成树形数组
    设计模式学习笔记
    Vue-cli项目部署到Nginx
    设计模式
    设计模式
    springboot原理
    类加载机制-深入理解jvm
    JVM内存调优原则及几种JVM内存调优方法
  • 原文地址:https://www.cnblogs.com/kudosharry/p/4735937.html
Copyright © 2020-2023  润新知