简体   繁体   English

在jQuery.ajax中为OpenCalais SemanticProxy服务指定回调

[英]Specifying a callback in jQuery.ajax to OpenCalais SemanticProxy service

I'm trying to query the OpenCalais service semanticproxy.com. 我正在尝试查询OpenCalais服务语义代理。 Unfortunately, their url format is as follows: 不幸的是,它们的url格式如下:

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=? 注意函数回调,不在callback =中吗? parameter, but rather follows the response format (jsonp:). 参数,而是遵循响应格式(jsonp :)。 This means that I can't use .getJSON, but rather need to use the .ajax method. 这意味着我不能使用.getJSON,而需要使用.ajax方法。 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. 但是,我真正想做的是设置Subject对象的属性。 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. 但是我不知道如何在Subject的上下文中创建一个可以用作回调的函数。 ie: 即:

Subject.prototype.handler = function(data) { this.title = data.title } 

Any ideas? 有任何想法吗?

You'd have to set a function on the window object. 您必须在window对象上设置一个函数。 This is essentially (I think) what jQuery does with its .getJSON method. 从本质上讲,(我认为)这就是jQuery使用.getJSON方法所做的事情。 The below is a bit hacky but hopefully it points you in the right direction: 以下内容有些古怪,但希望它可以为您指明正确的方向:

function Subject() {
}

Subject.prototype.populate = function(page_title) {
    // Save context object
    var subject = this;
    // Create function name like subjectHandler1281092055198
    var functionName = "subjectHandler" + new Date().getTime();
    window[functionName] = function(data) {
        // Invoke function with saved context and parameter
        subject.handler.call(subject, data);
    }
    var url = "http://service.semanticproxy.com/processurl/APIKEY/jsonp:" + functionName + "/http://en.wikipedia.org/wiki/" + page_title;
    $.ajax({url: url, dataType: "script", type: "GET", cache: false, callback: null, data: null});
};

Subject.prototype.handler = function (data) {
  // do stuff with the returned JSON
};

s = new Subject();
s.populate("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: 我不认为您能够做到这一点,仅仅是因为JSONP的工作原理,看看它实际上是如何返回浏览器的,它几乎可以做到这一点:

<script type="text/javascript">
  handler({ title: "Germany", ...other properties... });
</script>

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. 这里无法维护参考,您一次只能执行一个请求,也可以为每个主题保留一个对象映射,但是无法在JSONP请求中进行。

An object map would look something like this: 对象映射如下所示:

//delcare this once for the page
var subjects = {};

//do this per subject
var s = new Subject();
s.populate("Germany");
subjects["Germany"] = s;

Then in your hanldler, if any of the data properties is "Germany" , you could get it that way, for example: 然后,在您的hanldler中,如果任何data属性是"Germany" ,则可以通过这种方式获取它,例如:

var handler = function (data) {
  var subject = subjects[data.title];
  //subject is  your Germany subject, use it, go nuts!
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM