js bug: Origin 'null' is therefore not allowed access.

来源:互联网 发布:大数据时代影响 编辑:程序博客网 时间:2024/05/25 12:22

先说一下问题的由来,在本地html页面ajax请求本地或者局域网server的资源时遇到的bug,先来段出问题的代码:

<script type="text/javascript">    $.ajax({        type:"get",        url:"http://192.168.199.59:9000/api/v1/questions/5825acefffa2f815acdb03cf",        success:function(res){            console.log(888);        },        error:function(){            console.log(66666)        }    });</script>

翻译一下大家的解释好了:
Origin null is not allowed by Access-Control-Allow-Origin 意味着你在尝试ajax请求本地文件,因为安全的原因,这是被禁止的。
服务端的做法是在response内允许这么做:

response.setHeader("Access-Control-Allow-Origin", "*")

如果Chrome浏览器的话可以尝试加入新的插件:
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related

来个参考文献
Chrome Origin null is not allowed by Access-Control-Allow-Origin

0 0
原创粉丝点击