我使用的是firefox插件生成器。运行此代码时出现“未定义回调”错误
function callback(data) {
window.alert(data.status);
}
$.ajax({
url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=210e634a0b3af972daa908a447c735c1&start_date=201112&end_date=201112&jsonp=?",
dataType: "jsonp",
jsonp: "jsonp",
jsonpCallback: "callback"
});发布于 2012-01-23 16:23:03
我假设你是从一个内容脚本运行的。您必须考虑到,内容脚本并不真正在与网页脚本相同的上下文中运行-网页不能看到由内容脚本定义的函数,反之亦然(detailed description of this mechanism)。JSONP的工作原理是在网页中插入一个<script>标签。此脚本将在web页面的上下文中运行-它不会看到您在内容脚本中定义的回调函数。
要在窗口上下文中定义callback函数,请执行以下操作:
unsafeWindow.callback = function(data)
{
window.alert(data.status);
};但是,您应该认真对待warnings about unsafeWindow in the documentation,并尽可能避免使用它。使用扩展中的request package加载数据:
require("request").Request({
url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=210e634a0b3af972daa908a447c735c1&start_date=201112&end_date=201112",
onComplete: function(response)
{
console.log(response.json);
}
});然后您可以通过usual messaging将response.json发送到您的内容脚本。
发布于 2012-01-22 20:45:11
尝尝这个。
$.ajax({
url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=210e634a0b3af972daa908a447c735c1&start_date=201112&end_date=201112&jsonp=?",
dataType: "jsonp",
success: function(data) {
window.alert(data.status);
}
});发布于 2012-01-22 20:54:58
您不应该将jsonp=?附加到您的url,这是由ajax函数完成的。
仅使用:
url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=<your-api-key>&start_date=201112&end_date=201112",https://stackoverflow.com/questions/8961091
复制相似问题