Javascript 使用qwest.js到goo.gl url shortener api的AJAX post请求

Javascript 使用qwest.js到goo.gl url shortener api的AJAX post请求,javascript,jquery,ajax,google-url-shortener,Javascript,Jquery,Ajax,Google Url Shortener,我正在尝试使用开源库()中的goo.gl URL Shortener API来缩短URL。不过,我已经使用jquery成功地实现了它,但是当使用qwest完成时,它给我的错误是“此API不支持解析表单编码的输入” 我的jquery代码: var longURL = "http://www.google.com/"; $.ajax({ url: 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyANFw1rVq_v

我正在尝试使用开源库()中的goo.gl URL Shortener API来缩短URL。不过,我已经使用jquery成功地实现了它,但是当使用qwest完成时,它给我的错误是“此API不支持解析表单编码的输入”

我的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
                    });
强烈建议您提供任何帮助。

这里是qwest的作者;)

如文档中所述:
对于post和xhr2数据类型,默认内容类型标题为application/x-www-form-urlencoded,带有post请求

但谷歌Shortener服务不接受这一点。我假设它需要一个JSON输入类型。然后您应该将qwest的
dataType
选项设置为
json
。此外,您的
responseType
选项无效,不符合文档要求。通常,如果Google使用有效的
内容类型
标题答复请求,则不必设置它。下面是好代码:

qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&',
{longUrl:longUrl},
{dataType:'json'})


如果谷歌没有发送可识别的
内容类型
,只需将
responseType
选项也设置为
json

您好!感谢{dataType:'json'}。成功了。我也注意到我使用了一个错误的钥匙(不同的事情)好的插件汉克斯!很高兴您解决了问题。我们可以像使用ajax一样使用qwest进行同步调用吗?是的,我们可以,方法是将
async
选项设置为
false