dataType
is not for C# or MVC or server side, this is to tell jQuery that you are using json response and jQuery should parse it. Since you are not providing it, jQuery will not parse it and thus data is just an string. If you want json, then provide it or use JSON.parse
to parse your data string.
Either -
$.ajax({
url: "/DeviceUsage/GetNames",
type: "GET",
dataType: "json",
error: function (data) {
alert("wystąpił nieokreślony błąd " + data);
},
success: function (data) {
console.log(data);
availableTags = data;
}
})
or -
$.ajax({
url: "/DeviceUsage/GetNames",
type: "GET",
error: function (data) {
alert("wystąpił nieokreślony błąd " + data);
},
success: function (data) {
var jsonData = JSON.parse(data);
console.log(jsonData);
availableTags = jsonData;
}
})
And also, you are not waiting for the ajax to complete before you assign -
$("#deviceName").autocomplete({
source: availableTags
});
you should wait for ajax to complete, so take this call inside success like this -
success: function (data) {
//do proper parsing as mentioned earlier
availableTags = jsonData;
$("#deviceName").autocomplete({
source: availableTags
});
}
because at the moment of call availableTags
has nothing in it.