AJAX发布请求使用qwest.js到goo.gl url shortener api
AJAX post request using qwest.js to goo.gl url shortener api
我正在尝试使用开源库(qwest.js)goo.gl URL Shortener API来缩短URL。不过,我已经使用jquery成功地实现了这一点,但使用qwest时会出现错误"This 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的作者;)
如文件所述:the default Content-Type header is application/x-www-form-urlencoded for post and xhr2 data types, with a POST request
。
但是Google Shortener服务不接受它。我想它想要一个JSON输入类型。然后您应该将qwest的dataType
选项设置为json
。此外,您的responseType
选项无效,并且没有遵循文档。通常,如果Google使用有效的Content-Type
标头回复请求,则不必设置它。这是一个好代码:
qwest.post('https://www.googleapis.com/urlshortener/v1/url?key=479dfb502221d2b4c4a0433c600e16ba5dc0df4e&',
{longUrl: longURL},
{dataType:'json'})
在Google没有发送已识别的Content-Type
的情况下,只需将responseType
选项也设置为json
即可。
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 使用agility.js进行页面布局和合成
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 强制模板刷新ember.js
- 如何编写HTML输入的JS内联
- Angular JS IE9 Hashbang url rewriting
- 使用JS将数组转换为json对象
- Node.js v6.2.0类扩展不是函数错误
- 当js函数's已执行
- 要求未定义JS回调参数
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 如何从Java/scala调用js美化程序
- 如何更改<svg>标记为<img>用js标记
- 如何使用 node.js 比较两个 json 数组
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- Selectize.js:如何对整数值的选项进行排序
- AJAX发布请求使用qwest.js到goo.gl url shortener api