我正在构建一个小型客户端应用程序,该应用程序部署在使用 WYSIWYG CMS 构建的网站上。(不幸的是,我无权访问服务器)。
我正在使用 hash-bang 将应用程序的状态保存在 URL 中,并希望使用 TinyURL 的 create API 之类的东西来缩短它。本质上,我想用我的长 URL 作为请求来查询 3rd 方服务,并收到一个简短的响应。
我的问题是我不知道如何在没有收到 CORS 错误消息的情况下执行此操作:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.site-im-working-on.com' is therefore not allowed access.
这是我一直在尝试做的一个例子(使用jQuery):
var tinyAPI = 'http://tinyurl.com/api-create.php?url='; 函数 getTinyURL () { var longURL = window.location.href; var request = tinyAPI + longURL; 返回 $.get( 请求,函数(响应){ 返回响应; }); } // 在用户操作时获取微小的 URL $('.share-button').on('点击', function () { var tinyURL = tinyURL(); // 将字符串插入 DOM 元素 // 复制字符串到用户的剪贴板 // ETC... });
有没有办法只使用客户端代码来绕过CORS ?
(我也愿意使用另一个带有免费 API 的 URL 缩短器。)