我正在实现pretty simple API,并制作了一个简单的jQuery应用程序,用于构建本地URL,例如:
http://www.example.com?utm_campaign=test&utm_content=status&utm_source=facebook_saxocom&
当我调用API时,我使用这样的URL(更改了API密钥):
http://api.shortswitch.com/shorten?apiKey=MYAPIKEY&format=json&callback=?http%3A%2F%2Fwww.example.com%3Futm_campaign%3Dtest%26utm_content%3Dstatus%26utm_source%3Dfacebook_saxocom%26&longUrl=http%3A%2F%2Fapi.shortswitch.com%2Fshorten%3FapiKey%3D402aaa42317ba1e3b70214740dbec7a5d33a7522%26format%3Djson%26callback%3D%3Fhttp%253A%252F%252Fwww.example.com%253Futm_campaign%253Dtest%2526utm_content%253Dstatus%2526utm_source%253Dfacebook_saxocom%2526
当我在浏览器中打开它时,我得到了我想要的有效JSON(有和没有URl编码):
{
"results": {
"http://api.shortswitch.com/shorten?apiKey=MYAPIKEY&format=json&callback=?http%3A%2F%2Fwww.example.com%3Futm_campaign%3Dtest%26utm_content%3Dstatus%26utm_source%3Dfacebook_saxocom%26": {
"shortUrl": "http://s.domain.com/3M",
"hash": "3M",
"longUrl": "http://api.shortswitch.com/shorten?apiKey=MYAPIKEY&format=json&callback=?http%3A%2F%2Fwww.example.com%3Futm_campaign%3Dtest%26utm_content%3Dstatus%26utm_source%3Dfacebook_saxocom%26"
}
},
"errorCode": "0",
"errorMessage": "",
"statusCode": "OK"
}
我的通话功能:
function createNewShortUrl(url) {
$.getJSON(url, { longUrl: url }, function (data) {
if (data.statusCode == "OK") {
for (var i in data.results) {
$('#shortenUrl').val(data.results[i].shortUrl);
break;
}
return;
}
alert(results.errorMessage);
});
}
我从不进去:
$.getJSON(url, { longUrl: url }, function (data) {
As:在函数内部没有调用alert()函数,在Firebug调试器中我从未在其中找到断点。
所以我的问题是:我的getJSON函数出了什么问题?我应该更改什么才能获得""shortUrl": "http://s.domain.com/3M"
元素?
编辑 - 我的回复
在使用以下确切数据进行调用时,我得到200响应:
?http: //www.dr.dk?utm_campaign=fdfd&utm_content=status&utm_source=facebook_saxocom&({
"results": {
"http://api.shortswitch.com/shorten?apiKey=MYAPIKEY&format=json&callback=?http%3A%2F%2Fwww.dr.dk%3Futm_campaign%3Dfdfd%26utm_content%3Dstatus%26utm_source%3Dfacebook_saxocom%26": {
"shortUrl": "http://s.domain.com/3N",
"hash": "3N",
"longUrl": "http://api.shortswitch.com/shorten?apiKey=MYAPIKEY&format=json&callback=?http%3A%2F%2Fwww.dr.dk%3Futm_campaign%3Dfdfd%26utm_content%3Dstatus%26utm_source%3Dfacebook_saxocom%26"
}
},
"errorCode": "0",
"errorMessage": "",
"statusCode": "OK"
});