javascript 跨域 问题

发布时间:2016-09-09 15:15:27编辑:丝画阁阅读(1029)

XMLHttpRequest2 进行跨域访问时需要服务器许可,不是任何域都接受跨域请求的。先来看一下从 Yahoo YQL 域返回的响应头(Response Header ):

HTTP/1.1 200 OK
Set-Cookie: AO="o=1&s=1&dnt=1"; Version=1; Domain=yahoo.com; Max-Age=630720000; Expires=Sat, 18-Jun-2033 10:07:41 GMT; Path=/
Access-Control-Allow-Origin: *
Cache-Control: public, max-age=899
Content-Type: text/xml;charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sun, 23 Jun 2013 10:07:40 GMT

注意里面有一条标识 Access-Control-Allow-Origin:* ,这就表示允许跨域访问,所以可以正常访问该域,而对于其他没有该标识的域就会出现禁止访问提示。

 

那么如何设置呢?如果要接受跨域访问请求,就必须在服务器端返 回的资源中加入 Access-Control-Allow-Origin 头标识, Access-Control-Allow-Origin 的值可以是 URL 或 *,如果是 URL 则只会允许来自该 URL 的请求,* 则允许任何域的请求。比如,在 HTML 中可以设置:

<meta http-equiv="Access-Control-Allow-Origin" content="*">

或 

<meta http-equiv="Access-Control-Allow-Origin" content="http://www.baidu.com:80">


//header('Access-Control-Allow-Origin:http://A.abc.com');
//header('Access-Control-Allow-Origin:*');
//header('Access-Control-Allow-Credentials:true');

关键字