【发布时间】:2017-06-10 11:51:11
【问题描述】:
我正在尝试让 bit.ly API 正常工作,但我遇到了 CORS 问题。我检查了所有可能的来源,但问题仍然存在。
Angular2 代码:
getBitLyUrl (fullUrl : string): Observable<string> {
let headers = new Headers({'Access-Control-Allow-Origin' : '*', 'Accept' : 'application/json' });
let options = new RequestOptions({ headers: headers });
let res = this._http.get('https://api-ssl.bitly.com/v3/shorten?access_token=a4d0c60f2cfdba6941b9012c76d95a06f8b3765e&longUrl='
+ fullUrl + '&format=json', options)
.map((response: Response) => response.json());
return res;
}
来自 Chrome 网络的标头:
General:
Request URL:https://api-ssl.bitly.com/v3/shorten? access_token=a4d0c60f2cfdba6941b9012c76d95a06f8b3765e&longUrl=http://www.cyberma .net&format=json
Request Method:OPTIONS
Status Code:200 OK
Remote Address:67.199.248.20:443
Response:
Allow:GET, POST, OPTIONS
Connection:keep-alive
Content-Length:0
Content-Type:text/plain; charset=utf-8
Date:Wed, 25 Jan 2017 10:22:31 GMT
Server:nginx
Request:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en,cs;q=0.8,sk;q=0.6,de;q=0.4
Access-Control-Request-Headers:access-control-allow-origin, x-xsrf-token
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:api-ssl.bitly.com
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
我收到此错误:
XMLHttpRequest 无法加载 https://api-ssl.bitly.com/v3/shorten?access_token=a4d0c60f2cfdba6941b9012c76d95a06f8b3765e&longUrl=http://www.cyberma.net&format=json。 对预检请求的响应未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:3000” 访问。
任何帮助将不胜感激!
【问题讨论】: