API是否支持CORS? [英] Does bitly API support CORS?

查看:64
本文介绍了API是否支持CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请求此URL:

我收到标准错误:

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

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

推荐答案

如果仍然有此问题.发生这种情况的原因是,来自bitly的选项(预检)响应不包含"Access-Control-Allow-Origin"标头.

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.

为了解决这个问题,我使用了XMLHttpRequest,它不发送预检.

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

在bitlly的文档中实际上提到过

这篇关于API是否支持CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆