[英]calling method in javascript, but error throws?
I need to call ajax method couple of places. 我需要在几个地方调用ajax方法。 So want to try to minimize the code by creating separate method for it.
因此,要尝试通过为其创建单独的方法来尽量减少代码。 If use directly, it works perfect.
如果直接使用,效果很好。 but when I separate it won't work.
但是当我分开时,它将无法工作。
data: columns[5],
type: 'autocomplete',
options: { items: 100 },
source: function (query, process) {
$.ajax({
url: "/EditInitiatives.svc/GetLocationData?clientId=" + $value.val(),
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
query: query
},
success: function (response) {
process(response.d);
}
});
},
strict: true
}
it doesn't work, if I call this way. 如果我这样打电话,那是行不通的。 It says
Microsoft JScript runtime error: 'query' is undefined
, how to fix it? 它说
Microsoft JScript runtime error: 'query' is undefined
,如何解决?
{
data: columns[4],
type: 'autocomplete',
options: { items: 100 },
source: callAutoCompleteAjaxMethod(query, process, "/EditInitiatives.svc/GetLocationData?clientId=" + $value.val()),
strict: true
},
callAutoCompleteAjaxMethod = function (query, process, url) {
$.ajax({
url:url,
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
query: query
},
success: function (response) {
process(response.d);
}
});
},
You call 你打电话
source: callAutoCompleteAjaxMethod(query, ...
But you never gave 'query' a value, give it a value and it will work. 但是您从来没有给“查询”一个值,而是给它一个值,它将起作用。
You are calling the function instead of assigning it to the source property. 您正在调用该函数,而不是将其分配给source属性。 And at this moment the variable
query
is not defined. 并且目前还没有定义变量
query
。
You have to assign a function, so that the plugin can call it later: 您必须分配一个函数,以便该插件以后可以调用它:
source: function (query, process) {
callAutoCompleteAjaxMethod(
query,
process,
"/EditInitiatives.svc/GetLocationData?clientId=" + $value.val()
);
}
(I hope $value
is defined somewhere) (我希望在某个地方定义
$value
)
Parenthesis ( ()
) after a function reference always calls the function immediately. 函数引用后的括号(
()
)始终立即调用该函数。 If you want to pass a reference to the function, you don't put parenthesis after it. 如果要传递对该函数的引用,则不要在其后加上括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.