我正在尝试使用goo.gl API制作url shortener。但是,当我必须从JSON响应中获取短URL时,我坚持了下来!
在Chrome控制台中输入此代码后
var longURL = "http://stackoverflow.com/questions/ask"
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL +'"}',
dataType: 'json',
success: function(response) {
var result = JSON.parse(response);
}
});
我得到以下ouptut:
我发现我的短网址位于resoinseText.id
。如何从那里提取它?
答案 0 :(得分:2)
您无需调用JSON.parse()
,因为jQuery在您指定dataType: 'json'
时会自动执行此操作。您想要的值将位于id
的{{1}}属性中。
response