我正在尝试使用开源库(qwest.js)的 goo.gl URL Shortener API 来缩短 URL。虽然我已经使用 jquery 成功实现了它,但是它给了我错误“这个 API 不支持解析表单编码的输入”。使用 qwest 完成后。
我的 jquery 代码:
var longURL = "http://www.google.com/";
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl:"'+ longURL+'"}',
success: function(response) {
console.log(response)
}
})
.done(function(res) {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
以及带有 qwest.js 的非工作代码
var longURL = "http://www.google.com/"
qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&',
{longUrl: longURL},
{responseType:'application/json; charset=utf-8'})
.then(function(response) {
// Make some useful actions
})
.catch(function(e, url) {
// Process the error
});
强烈推荐任何帮助。