尽管请求与 jQuery getJSON 一起工作正常,但回调未执行
我正在尝试 获取资产列表(通过 JSON)使用免费的 drop.io 帐户在 drop 中。
请注意,我已经查看了其他两个问题(问题1和问题2)与此有关,但他们都没有提供解决方案。
var dropName = escape("greganddonny");
var apiKey = "some key I'm not displaying it here...";
var version = "2.0";
var assetsListBaseURL = "http://api.drop.io/drops/greganddonny/assets.json?api_key=" + apiKey + "&version=" + version;
var resultingdata = null;
function getFileList() {
console.log("BEGIN: getFileList()");
var surl = assetsListBaseURL + '&callback=?';
$.getJSON(surl, function(data, textStatus) {
console.log("textStatus:" + textStatus);
console.log("BEGIN: Callback()");
resultingdata = data;
showResult();
console.log("END: Callback()");
});
console.log("END: getFileList()");
}
function showResult()
{
alert(resultingdata);
}
$(document).ready(function() {
console.log("BEGIN: document.ready");
$.ajaxSetup({
"error": function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
})
$("#search").click(function() {getFileList();});
console.log("END: document.ready");
});
当单击页面上的搜索按钮时,将触发 getFileList() 函数。根据我的萤火虫控制台, getFileList() 函数被调用得很好。
当我在 drop.io 网站上调用 asset.json 时,Firebug 控制台的 Net 选项卡显示 200OK 状态,JSON 请求也已成功发出。我还在“Net”选项卡的请求列表中收到以下返回 JSON:
[{"converted":"http:\/\/drop.io\/download\/public\/x0yfnmzdtet1vtxesqcm\/12a9ebc74c9a3e4e3b2e2b4ea3546b919a2519ff\/df43d2b0-926f-012c-26c2-fbe072d62af6\/2a0a5270-9270-012c-8fc9-fbfd417303a8\/v2\/content","type":"audio","status":"converted","title":"MikeAndAndyOnPhone2009.MP3","duration":118,"artist":"Unknown","created_at":"2009/10/03 17:29:02 +0000","filesize":1895424,"description":null,"track_title":"Unknown","hidden_url":"http:\/\/drop.io\/hidden\/sazpcf7522exzb\/asset\/bWlrZWFuZGFuZHlvbnBob25lMjAwOS1tcDM=","name":"mikeandandyonphone2009-mp3"}]
我使用 JSONLint 并报告返回的 JSON 确实有效,您可以通过复制并粘贴上面的块来测试这一点。
我已经尝试了我知道如何尝试的所有方法,我什至在 $.ajaxSetup 方法中添加了一个“错误”选项,但我仍然没有成功返回甚至一条错误消息,说明为什么我的回调函数没有被执行叫。
知道为什么会这样吗?您可以在此处测试我的脚本
I am trying to obtain a list of assets (via JSON) inside of a drop using a free drop.io account.
Note that I have already looked at the other two questions (question1 and question2) concerning this and neither of them provided a solution.
var dropName = escape("greganddonny");
var apiKey = "some key I'm not displaying it here...";
var version = "2.0";
var assetsListBaseURL = "http://api.drop.io/drops/greganddonny/assets.json?api_key=" + apiKey + "&version=" + version;
var resultingdata = null;
function getFileList() {
console.log("BEGIN: getFileList()");
var surl = assetsListBaseURL + '&callback=?';
$.getJSON(surl, function(data, textStatus) {
console.log("textStatus:" + textStatus);
console.log("BEGIN: Callback()");
resultingdata = data;
showResult();
console.log("END: Callback()");
});
console.log("END: getFileList()");
}
function showResult()
{
alert(resultingdata);
}
$(document).ready(function() {
console.log("BEGIN: document.ready");
$.ajaxSetup({
"error": function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
})
$("#search").click(function() {getFileList();});
console.log("END: document.ready");
});
When the search button on the page is clicked this triggers the getFileList() function. According to my firebug console the getFileList() function is called just fine.
The JSON request is also made successfully as the firebug console's Net tab displays a 200OK status when I call assets.json on drop.io's site. I also receive the following return JSON in the list of requests in the "Net" tab:
[{"converted":"http:\/\/drop.io\/download\/public\/x0yfnmzdtet1vtxesqcm\/12a9ebc74c9a3e4e3b2e2b4ea3546b919a2519ff\/df43d2b0-926f-012c-26c2-fbe072d62af6\/2a0a5270-9270-012c-8fc9-fbfd417303a8\/v2\/content","type":"audio","status":"converted","title":"MikeAndAndyOnPhone2009.MP3","duration":118,"artist":"Unknown","created_at":"2009/10/03 17:29:02 +0000","filesize":1895424,"description":null,"track_title":"Unknown","hidden_url":"http:\/\/drop.io\/hidden\/sazpcf7522exzb\/asset\/bWlrZWFuZGFuZHlvbnBob25lMjAwOS1tcDM=","name":"mikeandandyonphone2009-mp3"}]
I validated the above block of returned JSON using JSONLint and it reports that the JSON returned is indeed valid, you can test this out by copying and pasting the block above.
I've tried everything I know how to try, I've even added an "error" option to the $.ajaxSetup method and I still haven't had any success returning even an error message as to why my callback function is not being called.
Any idea why this might be? You can test out my script here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSONP 要求服务器支持 JSONP 请求。但是,如果您访问
返回的文档,它看起来像
[{"converted..)
,尽管它应该看起来像x([{"converted"..)
因为x
回调。显然 drop.io 不支持 JSONP(文档也没有说明这一点)。留给您的唯一选择是跨域 js 的经典方法。 :使用代理。
JSONP requires the server to support JSONP requests. However if you visit
the document returned looks like
[{"converted..)
although it should look likex([{"converted"..)
because of thex
callback.Apparently drop.io doesn't support JSONP (documentation doesn't tell so either). The only option that's left for you is the classic approach to cross-domain js: Use a Proxy.