I'm trying to make some jQuery functions a little easier to implement and so I tried to encapsulate some autocomplete logic into a function to allow a user to send in a variable of the URL, the parameter of the web service and the control whose value we need to take. With the following script I get the error: response is not defined. The idea is that in this web service there will be many different methods which have autocomplete functionality and I can simply pass the name of the appropriate method and its parameter to the Complete method and have the functionality on multiple text boxes.
Why is it that
$(document).ready(function ()
{
$('#auto').autocomplete(
{
source: function (request, response)
{
Complete('GetNames', 'hospitalName', '#auto');
}
});
function Complete(url, varName, target)
{
$.ajax(
{
type: "POST",
url: "Service.asmx/" + url,
data: "{'" + varName + "':'" + $(target).val() + "'}",
dataType: "json",
contentType: "application/json",
success: function (data)
{
//uncaught reference error response is not defined
response(data.d);
},
error: function (xhr)
{
console.log(xhr.status);
}
});
}
});
This was working fine before I tried to take out the AJAX call and make it its own method. I'd like to know
- the source function is calling the Complete function and the source function has
requestandresponseas parameters, so why are they undefined in my script? - how can I fix this and avoid future problems in #1.
Completeis not within the body of thesource:function,responseis out of scope.