Does the bitry API support CORS?

I've seen in my documentation that CORS is supported. But my attempts to make a request from JavaScript are unsuccessful.

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 am getting 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

+3


source to share


1 answer


If anyone else has this problem. The reason this happens is because the default (preflight) response from the bit does not include the "Access-Control-Allow-Origin" header.

to get around this i used XMLHttpRequest which doesn't send preview.



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

      

This is actually mentioned in the bitly documentation

0


source







All Articles