问题描述
后端域名为A.abc.com,前端域名为B.abc.com。浏览器在访问时,会出现跨域访问。浏览器对于javascript的同源策略的限制。
HTTP请求时,请求本身会返回200,但是返回结果不会走success,并且会在浏览器console中提示:
已拦截跨源请求:同源策略禁止读取位于 https:///jquery/1.10.2/jquery.min.js"></script></head><body><button onclick="set()">set</button><br><br><button onclick="get()">get</button><script>function set(){ $.ajax({ url:'http://wencst.vicp.net/setString?value=10', xhrFields:{ withCredentials:true }, success:function(result){ alert(result); } });}function get(){ $.ajax({ url:'http://wencst.vicp.net/getString', xhrFields:{ withCredentials:true }, success:function(result){ alert(result); } });}</script></body></html>
html文件可以单独本地访问即可出现效果,并不一定要形成服务访问。
当服务端不允许跨域访问时,html文件访问均报错,并调用失败。
当服务端允许跨域访问时,html请求访问成功。
当服务端开启cookie传递,并在html文件中增加 xhrFields参数时,session生效。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。