I am quite new to programming, but most of it I am new when it comes to JavaScript/jQuery. The reason I am here is because even I have searched through the internet for a solution for my search with autocomplete / I have tryed to apply the different versions of what I found, I cannot get to a solution that actually works :)
Here is my piece of code:
var mydata
$(document).ready(function ()
{
ConstructSuggestionArray();
$("[id$='txtSearchProject']").keypress(function ()
{
$("[id$='txtSearchProject']").autocomplete
({
source: mydata
})
})
});
function ConstructSuggestionArray()
{
$.ajax
({
url: 'ProjectManagement.aspx/ConstructSuggestionArray',
type: "POST",
data: {},
async: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response)
{
if (response.d != null)
{
mydata = jQuery.parseJSON(response.d);
return true;
}
else
{
return false;
}
}
});
}
Also, the piece of code where I construct the array:
public string ConstructSuggestionArray()
{
using (DataClassesDataContext context = new DataClassesDataContext())
{
List<Utils.ProjectsOfAUser> theProjects =
ReturnProjectsAccordingToAllocation(context);
string[] projectsNameArray = new string[theProjects.Count];
int index = 0;
foreach (Utils.ProjectsOfAUser oneProject in theProjects)
{
projectsNameArray[index] = oneProject.Name;
index++;
}
string strJSON = string.Empty;
JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();
strJSON = objJSSerializer.Serialize(projectsNameArray).ToString();
return strJSON;
}
}
}
And I have also, added the script in my project and in my asp.net page.
I am very confused and I would be very thankful if you could help me figure out this.
Mention : txtSearchProject - is an asp control.
Thank you in advance.
dataType: 'json'in your jQuery, you probably shouldn't need to calljQuery.parseJSON()because yoursuccesscallback function should only ever be passed an object, not a string. If the response isn't valid JSON, thesuccesscallback won't be executed. Can you post the response from the AJAX call?