0

I need to call ajax method couple of places. 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?

{
    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);
                    }
                });
            },
1
  • 2
    What part of the error don't you understand? Commented Jun 10, 2013 at 20:51

2 Answers 2

2

You call

source: callAutoCompleteAjaxMethod(query, ...

But you never gave 'query' a value, give it a value and it will work.

Sign up to request clarification or add additional context in comments.

Comments

1

You are calling the function instead of assigning it to the source property. And at this moment the variable query is not defined.

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)

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.