js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
来源:互联网 发布:java.util.optional 编辑:程序博客网 时间:2024/06/08 01:03
js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
在本地用ajax跨域访问请求时报错:
XMLHttpRequest cannot loadhttp://www.zjblogs.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
查了一翻资料,发现原来是新W3C标准中是这样规定的:
最新的W3C标准里是这么实现HTTP跨域请求的, Cross-Origin Resource Sharing
简单的来说,就是跨域的目标服务器要返回一系列的Headers,通过这些Headers来控制是否同意跨域。这些Headers有:4 Syntax
4.1
Access-Control-Allow-Origin
HTTP Response Header4.2
Access-Control-Max-Age
HTTP Response Header4.3
Access-Control-Allow-Credentials
HTTP Response Header4.4
Access-Control-Allow-Methods
HTTP Response Header4.5
Access-Control-Allow-Headers
HTTP Response Header4.6
Origin
HTTP Request Header4.7
Access-Control-Request-Method
HTTP Request Header4.8
Access-Control-Request-Headers
HTTP Request Header
在 Request 包和 Response 包中都有一些。
其中最敏感的就是 Access-Control-Allow-Origin 这个 Header, 他是W3C标准里用来检查该跨域请求是否可以被通过。 (Access Control Check)
跨域实现的过程大致如下:
从 http://www.a.com/test.html 发起一个跨域请求,
请求的地址为: http://www.b.com/test.php
如果 服务器B返回一个如下的header
Access-Control-Allow-Origin: http://www.a.com
那么,这个来自 http://www.a.com/test.html 的跨域请求就会被通过。
如上所知,总结解决办法如下:
1、如果请求的url是aspx页面,则需要在aspx页面中添加代码:Response.AddHeader("Access-Control-Allow-Origin", "*");
2、如果请求的url是PHP页面,则需要在PHP页面中添加代码:header("Access-Control-Allow-Origin: *");
3、如果请求的url是静态的html页面,则需要在页面中添加meta标签代码:<meta http-equiv="Access-Control-Allow-Origin" content="*" />
如果服务器端可以确定是要被哪些域名访问,最好是能把以上代码中的“*”代替为具体的域名,这样做可以相应的增强安全性。
- js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
- js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
- js跨域访问,no 'access-control-allow-origin' header is present on the requested resource
- jQuery 跨域访问的三种方式 No 'Access-Control-Allow-Origin' header is present on the reque
- 本地Ajax跨域访问 No 'Access-Control-Allow-Origin' header is present on the requested resource.
- jQuery 跨域访问的三种方式 No 'Access-Control-Allow-Origin' header is present on the reque
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- (转)No 'Access-Control-Allow-Origin' header is present on the requested resource.'Ajax跨域访问解决方案
- 跨域问题:No 'Access-Control-Allow-Origin' header is present on the requested resource.
- ajax跨域请求:No 'Access-Control-Allow-Origin' header is present on the requested resource
- No 'Access-Control-Allow-Origin' header is present on the requested resource.ajax跨域请求
- 跨域问题:“No 'Access-Control-Allow-Origin' header is present on the requested resource”
- JS跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource
- js跨域No 'Access-Control-Allow-Origin' header is present---js跨域英文翻译是cross-origin
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore no
- Allow No 'Access-Control-Allow-Origin' header is present
- Mysql之无法查询中文字解决办法
- 避免Java内存溢出的注意事项
- Java中格式化输出数字
- C++第4次实验——输出星号图
- Android支付——支付宝支付总结
- js跨域访问,No ‘Access-Control-Allow-Origin‘ header is present on
- Linux网络编程入门
- JQuery中Ajax实现对Map,List集合数据的接收
- iOS开发-取消导航栏渲染问题(适应各种渲染问题)
- JAVA面试题二--转载自用
- leetcode——337—— House Robber III
- jcenter 的一点总结
- hdu 5667
- Spring Junit4 测试Web应用程序