怎么允许跨域的js访问服务端的接口

时间:2022-08-28 22:14:23

网上的文章一大把,不如两行代码来的实在

 

response.setHeader("Access-Control-Allow-Origin", "*"); //允许所有域名访问
response.setHeader("Access-Control-Allow-Origin", "http://www.123.com"); //允许www.123.com访问

Python 的实现

1         try:
2             content = json.dumps(ret)
3             request.setHeader("content-length", str(len(content)))
4             request.setHeader("Access-Control-Allow-Origin", "*") # 允许所有的域名访问
5             #request.setHeader("Access-Control-Allow-Origin", "http://www.123.com") # 允许指定域名访问
6             request.write(content)
7         except Exception, e:
8             SysLog(1,"TestResource._cbRender fail [%s]"%str(e))
9         request.finish()

PHP的例子

1 <?php  header('Access-Control-Allow-Origin:http://A.abc.com'); ?>