ajax跨域請求:No 'Access-Control-Allow-Origin' header is present on the requested resource
阿新 • • 發佈:2019-02-06
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", "*");
即可完美解決,至於安全性問題,後期再說。