Origin null is not allowed by Access-Control-Allow-Origin

转自:http://my.oschina.net/LinBandit/blog/33160

今天在研究js调用webservice时发现谷歌一直报:Origin null is not allowed by Access-Control-Allow-Origin

后来查到错误原因是:

使用xmlhttprequest获取服务数据时,在IE下能通过而在 chrome不能通过的问题,这就是所谓的跨域问题,什么是跨域呢?比较不规范但是通俗的说说法吧,比如你使 用chrome打开一个本地的html页面,那么我认为现在你的机器就是一个站点,这个html就是运行在这个站点上,如果你要在这个html页面中通过 xmlhttprequest获取百度的页面源码信息,就有点跨域的意思,这和你直接在地址栏上输入www.baidu.com又不一样,因为那样的话, 你当前的站点就是百度了,如果百度也在他的主页上通过xmlhttprequest获取google的页面信息,那样应该也算是跨域了吧,但是这个跨域问 题在服务器上是不存在的,你在服务器上使用urlHttpConnection连接其他站点是允许的。

可以想象,你连接到你自己的服务器,打开一个页面,如果这个页面要获取google的数据,那么这个页面可以向服务器发出请求(这个页面和这个服务 器是同一个域,我通俗的理解,所以这个请求没问题),服务器根据请求向google获取数据(这个是允许的),然后服务器再将获取到的数据反馈回页面,这 样页面就获取到google的数据了。

具体代码怎么写就不在话下啦!

最后可以通过服务器来解决这个问题,但至于为什么IE8可以允许跨域访问我就不清楚了!

=====================分隔符===============================

加料

转自:http://www.cnblogs.com/accessking/archive/2012/05/12.html

在学习XMLHttpRequest的使用过程中,使用chrome打开任意空网页,在控制台下输入如下代码

1 var oHttp=new XMLHttpRequest();
2 undefined
3 oHttp.open('GET','http://www.baidu.com/')
4 undefined
5 oHttp.send();
6 undefined
7 XMLHttpRequest cannot load http://www.baidu.com/. Origin null is not allowed by Access-Control-Allow-Origin.

报错部分的英文的大概意思就是不允许跨域,至于什么叫做不允许跨域呢?

请看上面解释。

我尝试了chrome、firfox、opera都出现了这种情况,但是使用IE8就不会出现这种错误,很多人不是很理解。

这里Windows help and support 做出了解释  IE本身也并不是允许跨域的因为这样很不安全,但是如果不跨域的话又带来很多不便,所以会让你手动的选择一下是否允许跨域。而chrome、firfox、opera则默认就是不允许。

What is active content and why does Internet Explorer restrict it?

Active content is interactive or animated content used on websites. It includes ActiveX controls and web browser add-ons, which are small programs that are used extensively on the Internet. Active content can make web browsing more enjoyable by providing toolbars, stock tickers, video, animated content, and more.

Why does Internet Explorer restrict active content?

Internet Explorer restricts this content because occasionally these programs can malfunction or give you content you don't want. In some cases, these programs can be used to collect information about you, damage information on your computer, install software without your consent, or allow someone else to control your computer remotely. Given these risks, you should allow active content only if you completely trust the publisher or the website it's coming from.

How can I allow active content?

If Internet Explorer restricts active content that you are sure you want to allow, click the gold Information bar that appears at the top of the webpage, and then click Allow blocked content. For more information, see Internet Explorer Information bar: frequently asked questions.

posted @ 2012-12-10 00:17  horizon~~~  阅读(4344)  评论(0编辑  收藏  举报