jQuery令牌输入不处理跨域结果 [英] jQuery token-input not processing cross-domain results
问题描述
我创建的Chrome扩展程序使用 http://loopj.com/jquery-tokeninput/添加令牌,请参见上一个问题。
I'm creating a chrome extension which uses http://loopj.com/jquery-tokeninput/ to add tokens, see previous question.
我对如何从我的服务器获取要由tokenInput处理的结果感到困惑。以下文章(什么是JSONP?)表明,我需要添加回调查询参数以获取跨域jsonp工作:
I am confused as to how to get results from my server to be processed by tokenInput. The following article, What is JSONP?, suggests that I need to add a callback query param to get cross-domain jsonp working:
$(function() {
$("#token").tokenInput("http://localhost/token/search?callback=jsonprocess", {
preventDuplicates: true,
crossDomain: true,
});
});
这用于将响应包装在我的PHP代码中:
This is used to wrap the response in my php code:
header('Content-type: text/javascript');
echo $this->request->query('callback') . '(' . json_encode($token_array) . ')';
exit;
然后调用 jsonprocess()
方法在我的javascript。但是这是在tokenInput实例的上下文,所以我不能填充结果。这是正确的功能吗?
Which then calls the jsonprocess()
method in my javascript. However this is out of context of the tokenInput instance so I can't populate the results. Is this the correct functionality? Or is there a way to make the jQuery tokeninput plugin process the jsonp directly?
tokeninput中的成功回调:
The success callback in tokeninput:
ajax_params.success = function(results) {
cache.add(cache_key, $(input).data("settings").jsonContainer ? results[$(input).data("settings").jsonContainer] : results);
if($.isFunction($(input).data("settings").onResult)) {
results = $(input).data("settings").onResult.call(hidden_input, results);
}
};
...从不调用。
推荐答案
比我想象的容易;不需要在搜索网址中?callback = jsonprocess
Easier than I thought; don't need ?callback=jsonprocess
in the search url
$(function() {
$("#token").tokenInput("http://localhost/token/search", {
preventDuplicates: true,
crossDomain: true,
});
});
这篇关于jQuery令牌输入不处理跨域结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!