AJAX post 请求使用 qwest.js 到 goo.gl url 缩短器 api
AJAX post request using qwest.js to goo.gl url shortener api
我正在尝试使用 goo.gl URL Shortener API 开源库 (qwest.js) 来缩短 URL。虽然我已经使用 jquery 成功实现了它,但是当使用 qwest 完成时它给了我错误 "This API does not support parsing form-encoded input."。
我的代码 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 的作者 ;)
如文档中所述:the default Content-Type header is application/x-www-form-urlencoded for post and xhr2 data types, with a POST request
.
但是Google 缩短服务不接受它。我假设它需要 JSON 输入类型。那么你应该将qwest的dataType
选项设置为json
。此外,您的 responseType
选项无效并且不符合文档。通常,如果 Google 使用有效的 Content-Type
header 回复请求,则不必设置它。这是好的代码:
qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&',
{longUrl: longURL},
{dataType:'json'})
在 Google 没有发送识别的 Content-Type
的情况下,只需将 responseType
选项也设置为 json
。
我正在尝试使用 goo.gl URL Shortener API 开源库 (qwest.js) 来缩短 URL。虽然我已经使用 jquery 成功实现了它,但是当使用 qwest 完成时它给了我错误 "This API does not support parsing form-encoded input."。
我的代码 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 的作者 ;)
如文档中所述:the default Content-Type header is application/x-www-form-urlencoded for post and xhr2 data types, with a POST request
.
但是Google 缩短服务不接受它。我假设它需要 JSON 输入类型。那么你应该将qwest的dataType
选项设置为json
。此外,您的 responseType
选项无效并且不符合文档。通常,如果 Google 使用有效的 Content-Type
header 回复请求,则不必设置它。这是好的代码:
qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&',
{longUrl: longURL},
{dataType:'json'})
在 Google 没有发送识别的 Content-Type
的情况下,只需将 responseType
选项也设置为 json
。