This is a migrated thread and some comments may be shown as answers.

$.getJSON/Ajax call REST API to return array length

0 Answers 42 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
swati
Top achievements
Rank 1
Veteran
swati asked on 20 Aug 2020, 10:42 PM

Hi, I have a perfectly working code for auto populate textbox via rest api(it populates after 2 characters typed).  Can you assist me on modifying the same api code to find out the total length of array(/total companies) and assign to textbox val something like $("#TotalCompany").val(...) I currently have autopopulate companies $("#Company").autocomplete working just fine.

$(document).ready(function () {
                      var baseServiceUrl = 'http://services.appserver.com/';
                      $("#Company").autocomplete({
                                         minLength: 2,
                                         source: function (request, response) {
                                          $.getJSON(baseServiceUrl + '/company/name/' + request.term + '/?callback=?',
                                          {
                                                       contextKey: function () {
                                                      // applies to associated parties
                                                     var key = $(this).parent().parent().find('.type').val();
                                                     if (key !== "--Select--" && key !== "")
                                                              key = key === "O" ? "Company" : "Company";
                                                      else
                                                              key = "";
                                                     return key;
                                             },
                                             format: "json"
                                       },
                                       function (data) {
                                                         response($.map(data,
                                                         function (item) {
                                                                                   return {
                                                                                                   label: item['Company'].replace(/&/g, '&'),
                                                                                                   value: item['Company'].replace(/&/g, '&')
                                                                                                }
                                            }));
                          }).sort();
                 }

        });
});

The web service API return something simillar to the script below:
<ArrayOfCompany xmlns="http://schemas.datacontract.org/2004/07/CR.WebServices.Model.EntityFramework" xmlns:i=http://www.w3.org/2001/XMLSchema-instance>
<Company>…</Company>
<Company>…</Company>
<Company>…</Company>
</ArrayOfCompany>

Please let me know if you need more details Greatly appreciated any help

No answers yet. Maybe you can help?

Tags
Ajax
Asked by
swati
Top achievements
Rank 1
Veteran
Share this question
or