Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery.ajax中指定对OpenCalais SemanticProxy服务的回调_Javascript_Jquery_Ajax_Jsonp - Fatal编程技术网

Javascript 在jQuery.ajax中指定对OpenCalais SemanticProxy服务的回调

Javascript 在jQuery.ajax中指定对OpenCalais SemanticProxy服务的回调,javascript,jquery,ajax,jsonp,Javascript,Jquery,Ajax,Jsonp,我正在尝试查询OpenCalais服务semanticproxy.com。不幸的是,他们的url格式如下: http://service.semanticproxy.com/processurl/APIKEY/jsonp:handler_function/http://en.wikipedia.org/wiki/Germany 请注意,函数callback不在callback=?参数,但遵循响应格式(jsonp:)。这意味着我不能使用.getJSON,而是需要使用.ajax方法。因此,我有以下

我正在尝试查询OpenCalais服务semanticproxy.com。不幸的是,他们的url格式如下:

http://service.semanticproxy.com/processurl/APIKEY/jsonp:handler_function/http://en.wikipedia.org/wiki/Germany
请注意,函数callback不在callback=?参数,但遵循响应格式(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");
这个很好用。但我真正想做的是设置我的主题对象的属性。但是我不知道如何在主题的上下文中创建一个函数,该函数可以用作回调函数。i、 e:

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

有什么想法吗?

我不认为你能做到这一点,仅仅因为JSONP是如何工作的,看看它实际上是如何回到浏览器的,它基本上是这样做的:

<script type="text/javascript">
  handler({ title: "Germany", ...other properties... });
</script>
然后,在您的hanldler中,如果任何
数据
属性是
“Germany”
,您可以通过这种方式获得它,例如:

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

我不认为你能做到这一点,仅仅因为JSONP是如何工作的,看看它实际上是如何回到浏览器的,它基本上是这样做的:

<script type="text/javascript">
  handler({ title: "Germany", ...other properties... });
</script>
然后,在您的hanldler中,如果任何
数据
属性是
“Germany”
,您可以通过这种方式获得它,例如:

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

您必须在
窗口
对象上设置一个函数。这就是jQuery使用其.getJSON方法的本质(我认为)。下面的内容有点粗糙,但希望它能为您指明正确的方向:

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");

您必须在
窗口
对象上设置一个函数。这就是jQuery使用其.getJSON方法的本质(我认为)。下面的内容有点粗糙,但希望它能为您指明正确的方向:

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");

重新仔细阅读这个问题,我和你第一次读的完全一样……他的回调有效,他想知道如何引用调用填充方法的
主题。谢谢roryf。我想这给了我一个主题,但正如尼克所说,不是主题。根据您提供的见解,我又看了一眼jQuery源代码(第一次让我大吃一惊),似乎就是这样。因此,即使使用普通的“callback=?”,您也永远无法获得主题。谢谢。它使用正确的上下文在原始的
主题
实例上执行
处理程序
函数,以便在该函数中您可以使用
this
访问所有对象属性。这不是你想要的吗?事实上是的,对不起roryf。我测试了它,但在测试代码中出现了一个错误。可能该睡觉了。为此干杯!重新仔细阅读这个问题,我和你第一次读的完全一样……他的回调有效,他想知道如何引用调用填充方法的
主题。谢谢roryf。我想这给了我一个主题,但正如尼克所说,不是主题。根据您提供的见解,我又看了一眼jQuery源代码(第一次让我大吃一惊),似乎就是这样。因此,即使使用普通的“callback=?”,您也永远无法获得主题。谢谢。它使用正确的上下文在原始的
主题
实例上执行
处理程序
函数,以便在该函数中您可以使用
this
访问所有对象属性。这不是你想要的吗?事实上是的,对不起roryf。我测试了它,但在测试代码中出现了一个错误。可能该睡觉了。为此干杯!谢谢你,尼克。很高兴在我尝试构建比测试脚本更多的东西之前,我已经学会了罐头和罐头。再次感谢,谢谢尼克。很高兴在我尝试构建比测试脚本更多的东西之前,我已经学会了罐头和罐头。再次感谢。