scDisorder scDisorder - 6 months ago 61
Javascript Question

ajax jquery autocomplete get data from asmx

I met the big one problem with jQuery autocomplete with source .asmx file
here's my code:

$("#enterprise_search").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Services/EnterprisePortal/wsGetFAQQuestions.asmx/GetQuestionsByWord",
type: "GET",
contentType: "application/json; charset=utf-8",
data: "Word="+$('#enterprise_search').val(),
dataType: "json",
success: function (data) {
console.log('Data recieved');
response($.map(data.d, function () {
return {
label: item.Name + '(' + item.Value + ')',
value: item.Name
}
}))
},
error: function (xhr, msg) {
console.log('Database connect error: ' + msg);
}
});
},
minLength: 1,
select: function (e, ui) {
var result = item.Name;
var answer = item.Value;
$('#search-results').append('<p>' + result + ' : ' + answer + '</p>');
},
close: function () {
$("#enterprise_search").val('');
}
});


there is a method of c# or whatever it is (that code is not mine, just for you to take a look)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] //Specify return format.
public string GetQuestionsByWord(string Word)
{
//JavaScriptSerializer YourSerializer = new JavaScriptSerializer();
//return YourSerializer.Serialize(FAQsCOL);


Dictionary<string, string> FAQsCOL = clsFAQBLL.GetFAQCOLByWordInQuestionAnswer(Word);
//Dictionary<string, string> ReturnLinks = FAQsCOL.ToDictionary(m => string.Format("{0}?{1}={2}", clsParameters.clsPages.ENTERPRISE_PORTAL_FAQs, clsParameters.clsQueryString.FAQ_ID, m.Key), m => m.Value);
return JsonConvert.SerializeObject(FAQsCOL);
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] //Specify return format.

public string GetQuestionByQuestionId(int FAQId)
{
Linq.General.FAQ CurrentFAQ = clsFAQBLL.GetFAQByFAQId(FAQId);
FAQ FAQ = new FAQ();
if (CurrentFAQ != null)
{
FAQ = new FAQ(CurrentFAQ.Question, CurrentFAQ.Answer);
}
return JsonConvert.SerializeObject(FAQ);
}


after all of these I get in console (search for '2'):
jquery-2.1.4.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in {"5":"שאלה - 17","11":"מהו הנוהל לגבי מתמחה אשר מנהלו מסרב לחתום על טופס ההרשמה לבחינה?","20":"מהו נוהל הזכאות לגשת לבחינות התמחות?"}


i tried to push data into array (search for '1'), that's the look:
d
:
"{"2":"שאלה 2","3":"שאלה 2","4":"שאלה 2","11":"מהו הנוהל לגבי מתמחה אשר מנהלו מסרב לחתום על טופס ההרשמה לבחינה?","20":"מהו נוהל הזכאות לגשת לבחינות התמחות?"}"


can anyone help me with this? I've lost a lot of time for this sick thing
can't understand is it a mistake or something in webservice or in my jquery function

Answer

And again after couple of hours mind**ng gives me an answer by myself:

var searchValue = JSON.stringify($('#enterprise_search').val());

data: {Word:searchValue},

success: function (datas) {
            console.log('Data recieved: ' + datas.d);
            var data = JSON.parse(datas.d);
            $.each(data, function (index, value) {
                $('#search-results').append('p' + index + ': ' + value + '</p>');
            });
        }

hope helps someone, cause I'm not the best on this thing, but shall be ;)

Comments