Javascript Select2:未捕获类型错误:options.results不是函数
我正在尝试使用Select2jQuery插件进行AJAX调用。查询似乎正在工作,但在Javascript Select2:未捕获类型错误:options.results不是函数,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,我正在尝试使用Select2jQuery插件进行AJAX调用。查询似乎正在工作,但在选项对象上调用.results()时会出现问题: 未捕获类型错误:options.results不是函数 这是我的HTML: <input class="form-control" type="number" value="2125" name="topic_relation[source_topic_id]" id="topic_relation_source_topic_id" /> 以下是失败
选项
对象上调用.results()
时会出现问题:
未捕获类型错误:options.results不是函数
这是我的HTML:
<input class="form-control" type="number" value="2125" name="topic_relation[source_topic_id]" id="topic_relation_source_topic_id" />
以下是失败的代码:
function ajax(options) {
var timeout, // current scheduled but not yet executed request
handler = null,
quietMillis = options.quietMillis || 100,
ajaxUrl = options.url,
self = this;
return function (query) {
window.clearTimeout(timeout);
timeout = window.setTimeout(function () {
var data = options.data, // ajax data function
url = ajaxUrl, // ajax url string or function
transport = options.transport || $.fn.select2.ajaxDefaults.transport,
// deprecated - to be removed in 4.0 - use params instead
deprecated = {
type: options.type || 'GET', // set type of request (GET or POST)
cache: options.cache || false,
jsonpCallback: options.jsonpCallback||undefined,
dataType: options.dataType||"json"
},
params = $.extend({}, $.fn.select2.ajaxDefaults.params, deprecated);
data = data ? data.call(self, query.term, query.page, query.context) : null;
url = (typeof url === 'function') ? url.call(self, query.term, query.page, query.context) : url;
if (handler && typeof handler.abort === "function") { handler.abort(); }
if (options.params) {
if ($.isFunction(options.params)) {
$.extend(params, options.params.call(self));
} else {
$.extend(params, options.params);
}
}
$.extend(params, {
url: url,
dataType: options.dataType,
data: data,
success: function (data) {
========> var results = options.results(data, query.page, query); <==========
query.callback(results);
},
error: function(jqXHR, textStatus, errorThrown){
var results = {
hasError: true,
jqXHR: jqXHR,
textStatus: textStatus,
errorThrown: errorThrown
};
query.callback(results);
}
});
handler = transport.call(self, params);
}, quietMillis);
};
}
函数ajax(选项){
var timeout,//当前已计划但尚未执行的请求
handler=null,
quietMillis=options.quietMillis | | 100,
ajaxUrl=options.url,
self=这个;
返回函数(查询){
clearTimeout(超时);
timeout=window.setTimeout(函数(){
var data=options.data,//ajax数据函数
url=ajaxUrl,//ajaxUrl字符串或函数
transport=options.transport | |$.fn.select2.ajaxDefaults.transport,
//已弃用-将在4.0中删除-改用参数
不推荐={
type:options.type | |‘GET’,//设置请求类型(GET或POST)
cache:options.cache | | false,
jsonpCallback:options.jsonpCallback | |未定义,
数据类型:选项。数据类型| |“json”
},
params=$.extend({},$.fn.select2.ajaxDefaults.params,已弃用);
data=data?data.call(self,query.term,query.page,query.context):null;
url=(TypeOfURL=='function')?url.call(self、query.term、query.page、query.context):url;
if(handler&&typeof handler.abort==“函数”){handler.abort();}
if(options.params){
if($.isFunction(options.params)){
$.extend(params,options.params.call(self));
}否则{
$.extend(params,options.params);
}
}
$.extend(参数{
url:url,
dataType:options.dataType,
数据:数据,
成功:功能(数据){
====>var results=options.results(data,query.page,query);由于插件调用results()
,您还应该声明results:function(data,page)
而不是processResults:function(data,page)
Hi。results
正在调用插件的JS。在我的代码中,我确实调用了processResults()
我没有试图更改插件的代码……但我只是更改了代码,说results:function(数据,页面)
这似乎已经解决了问题..现在我遇到了另一个问题,尽管未捕获类型错误:无法读取未定义的属性“toUpperCase”
好的,我已经解决了我的问题。如果您可以推翻您的建议..我将把答案归功于您。谢谢这需要是您的答案:您需要编写结果:函数(数据,页面)
而不是processResults:function(数据,页面)
我也注意到了这一点。它们的文档与其实际功能之间没有联系。processResults->results.params.term和params.page甚至不存在。
{"total":2, "topics":[{"id":305,"name":"Educational Assessment, Testing, And Measurement"},{"id":3080,"name":"Inspectors, Testers, Sorters, Samplers, And Weighers"}]}
function ajax(options) {
var timeout, // current scheduled but not yet executed request
handler = null,
quietMillis = options.quietMillis || 100,
ajaxUrl = options.url,
self = this;
return function (query) {
window.clearTimeout(timeout);
timeout = window.setTimeout(function () {
var data = options.data, // ajax data function
url = ajaxUrl, // ajax url string or function
transport = options.transport || $.fn.select2.ajaxDefaults.transport,
// deprecated - to be removed in 4.0 - use params instead
deprecated = {
type: options.type || 'GET', // set type of request (GET or POST)
cache: options.cache || false,
jsonpCallback: options.jsonpCallback||undefined,
dataType: options.dataType||"json"
},
params = $.extend({}, $.fn.select2.ajaxDefaults.params, deprecated);
data = data ? data.call(self, query.term, query.page, query.context) : null;
url = (typeof url === 'function') ? url.call(self, query.term, query.page, query.context) : url;
if (handler && typeof handler.abort === "function") { handler.abort(); }
if (options.params) {
if ($.isFunction(options.params)) {
$.extend(params, options.params.call(self));
} else {
$.extend(params, options.params);
}
}
$.extend(params, {
url: url,
dataType: options.dataType,
data: data,
success: function (data) {
========> var results = options.results(data, query.page, query); <==========
query.callback(results);
},
error: function(jqXHR, textStatus, errorThrown){
var results = {
hasError: true,
jqXHR: jqXHR,
textStatus: textStatus,
errorThrown: errorThrown
};
query.callback(results);
}
});
handler = transport.call(self, params);
}, quietMillis);
};
}