Does bitly API support CORS?

假装没事ソ 提交于 2020-03-02 06:23:06

问题


I saw in their documentation that CORS is supported. But my attempts to make request from JavaScript have no success.

Requesting this URL:

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

I receive standard error :

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

UPD: bitly documentation - http://dev.bitly.com/cors.html


回答1:


If anyone still having this issue. The reason this happen is because the option (preflight) response from bitly doesn't include 'Access-Control-Allow-Origin' header.

to get around this I used XMLHttpRequest which doesn't send preflight.

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();

It is actually mentioned in bitlly's documentation



来源:https://stackoverflow.com/questions/26141578/does-bitly-api-support-cors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!