ajax跨域请求报错解决办法
<script type="text/javascript"> $(document).ready(function () { $.ajax({ type : "POST", url : "http://127.0.0.1:9999/app/getToken.ya", success : function(data) { var token = eval(data).rows[0]; window.location.href="http://10.6.2.26:8080/BOE/OpenDocument/opendoc/openDocument.jsp?token="+token; } }); }) </script>
报错:[Web浏览器] "XMLHttpRequest cannot load http://127.0.0.1:9999/app/getToken.ya. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access." /HelloHBuilder/index.html (0)
原因是ajax跨域请求被服务器端拒绝。只需要在服务器端加入:
response.setHeader("Access-Control-Allow-Origin", "*");
即可完美解决,至于安全性问题,后期再说。