- A+
所属分类:Web前端
最近在做一个项目需要进行token验证需要进行跨域请求,又不想去改动后台的代码,所以前端进行了请求,还好是Nginx代理的比较好处理,只需要在在http节点下添加三条命令就行了,命令如下:
add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; add_header Access-Control-Allow-Headers X-Requested-With;
前端处理代码
首先是取URl里面的值
<script language="javascript"> function GetRequest() { var url = window.location.search; //获取url中"?"符后的字串 console.log(url,'yuans') var theRequest = new Object(); console.log(url,'213'); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]); } } return theRequest; } </script>
然后再去发起请求
<script language="javascript"> $(function () { var Request = new Object(); Request = GetRequest(); var userid, token; userid = Request['userid']; token = Request['token']; console.log(Request, 'acc'); console.log(userid, 'acc1'); $.ajax({ type:"get", url: "http://xxx",// 请求的url地址 data:{'userid':userid,'token':token }, async: true, dataType: "xml", success: function (xml) { var name = $(xml).find('string').text();//获取xml里面的数据,string为标签名称 console.log(xml,'data') console.log(name, 'cxs'); } }) }) </script>
到此已经取到了验证token返回的值