2014-10-01 121 views
3

我在他们的文档中看到CORS被支持。但是我尝试从JavaScript发出请求没有成功。API是否支持CORS?

请求这个网址:

https://api-ssl.bitly.com/v3/shorten?longUrl=https://google.com/&access_token=token&domain=bit.ly&format=json&languageCode=en-US

我收到标准错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://google.com' is therefore not allowed access. 

UPD: bitly文档 - http://dev.bitly.com/cors.html

+0

你可能想链接到你所谈论的文档。 – Quentin 2014-10-01 12:40:16

+3

如果他们的文档与实际情况不符,那么您应该与他们的技术支持团队合作。 – Quentin 2014-10-01 12:41:25

回答

0

如果有人仍然有这个问题。发生这种情况的原因是因为来自Bitly的选项(预检)响应不包括“Access-Control-Allow-Origin”标题。

解决此问题我使用不发送预检的XMLHttpRequest。

let xhr = new XMLHttpRequest(); 
let url = 'https://api-ssl.bitly.com/v3/shorten?longUrl=https://google.com/&access_token=token' 
xhr.open('GET', url); 
xhr.onreadystatechange = function() { 
    if(xhr.readyState === 4) { 
    if(xhr.status===200) { 
     console.log(xhr.responseText) 
    } else { 
     console.log(xhr) 
    } 
    } 
}; 
xhr.send(); 

它在实际上提到bitlly的documentation