在 jQuery.ajax 中指定 OpenCalais SemanticProxy 服务的回调
我正在尝试查询 OpenCalais 服务 Semanticproxy.com。不幸的是,它们的 url 格式如下:
http://service.semanticproxy.com/processurl/APIKEY/jsonp:handler_function/http://en.wikipedia.org/wiki/Germany
请注意,回调函数不在回调=?参数,而是遵循响应格式 (jsonp:)。这意味着我不能使用 .getJSON,而是需要使用 .ajax 方法。所以我有以下对象定义:
function Subject() {
}
Subject.prototype.populate = function(page_title) {
var url = "http://service.semanticproxy.com/processurl/APIKEY/jsonp:handler/http://en.wikipedia.org/wiki/" + page_title;
$.ajax({url: url, dataType: "script", type: "GET", cache: false, callback: null, data: null});
};
var handler = function (data) {
// do stuff with the returned JSON
};
s = new Subject();
s.populate("Germany");
这工作正常。但我真正想做的是设置我的主题对象的属性。但我不知道如何在主题上下文中创建一个能够用作回调的函数。即:
Subject.prototype.handler = function(data) { this.title = data.title }
有什么想法吗?
I'm trying to query the OpenCalais service semanticproxy.com. Unfortunately, their url format is as follows:
http://service.semanticproxy.com/processurl/APIKEY/jsonp:handler_function/http://en.wikipedia.org/wiki/Germany
notice that the function callback, is not in a callback=? parameter, but rather follows the response format (jsonp:). This means that I can't use .getJSON, but rather need to use the .ajax method. So I have the following object definition:
function Subject() {
}
Subject.prototype.populate = function(page_title) {
var url = "http://service.semanticproxy.com/processurl/APIKEY/jsonp:handler/http://en.wikipedia.org/wiki/" + page_title;
$.ajax({url: url, dataType: "script", type: "GET", cache: false, callback: null, data: null});
};
var handler = function (data) {
// do stuff with the returned JSON
};
s = new Subject();
s.populate("Germany");
This works fine. But what I really want to do is set properties of my Subject object. But I don't know how to create a function in the context of the Subject that will be able to be used as the callback. i.e:
Subject.prototype.handler = function(data) { this.title = data.title }
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在
window
对象上设置一个函数。 (我认为)这本质上就是 jQuery 对其 .getJSON 方法所做的事情。下面的内容有点hacky,但希望它能为您指明正确的方向:You'd have to set a function on the
window
object. This is essentially (I think) what jQuery does with its .getJSON method. The below is a bit hacky but hopefully it points you in the right direction:我不认为你能够做到这一点,只是因为 JSONP 是如何工作的,看看它实际上是如何返回到浏览器的,它几乎是这样做的:
这里没有办法维护引用,你可以一次执行一个请求,或者为每个主题保留一个对象映射,但无法在 JSONP 请求中执行此操作。
对象映射看起来像这样:
然后在您的处理程序中,如果任何
data
属性是"Germany"
,您可以通过这种方式获取它,例如:I don't think you're going to be able to do this, just because of how JSONP works, look at how it actually comes back to the browser, it pretty much does this:
There's no way to maintain a reference here, you could do one request at a time, or keep an object map for each subject, but there's no way to do it in the JSONP request.
An object map would look something like this:
Then in your hanldler, if any of the
data
properties is"Germany"
, you could get it that way, for example: