简体   繁体   English

javascript中调用方法,但是会引发错误?

[英]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.

相关问题 Javascript,调用Java Applet方法抛出TypeError - Javascript, Calling Java Applet method throws TypeError ZeroClipboard JavaScript库引发未捕获的错误:悬停在Flash对象上时,在NPObject上调用方法出错 - ZeroClipboard JavaScript library throws Uncaught Error: Error calling method on NPObject while hovering on the flash object Javascript方法可用,但在调用该方法时会引发异常 - Javascript method is available but throws exception when calling the method 在NPObject上调用方法出错! 用Java语言编写 - Error calling method on NPObject! in Javascript 调用导入的类'静态方法会引发TypeError错误 - Calling imported class' static method throws a TypeError error 反应:在render方法中调用setState会引发错误 - React: Calling setState within render method throws error 未捕获的错误:javascript中NPObject上的错误调用方法 - Uncaught Error: Error calling method on NPObject in javascript 从Javascript的aspx背后的Codebeing调用函数引发错误 - Calling Function in Codebehind of aspx from Javascript Throws Error 错误的JavaScript方法未定义使用PHP调用 - error javascript method is not defined calling using php Javascript - > Flash throw“NPObject上的错误调用方法” - Javascript -> Flash throwing “Error calling method on NPObject”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM