jQuery每个具有单个对象的循环都返回"undefined"(未定义). [英] jQuery each loop with single object returns "undefined"

查看:138
本文介绍了jQuery每个具有单个对象的循环都返回"undefined"(未定义).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery函数,可以调用ASP.NET Web API.我知道API成功返回了客户端对象,因为我可以直接输入API网址,并且对象信息以XML格式显示在浏览器中. JSON数据作为集合返回,并且jQuery循环遍历并将<option>元素附加到<select>元素.

I have a jQuery function that makes a call to an ASP.NET Web API. I know the API returns a client object successfully because I can enter the API url directly and the object info is displayed in XML format in the browser. The JSON data is returned as a collection and jQuery loops through and appends <option> elements to a <select> element.

我遇到的麻烦是如何使用jQuery显示结果. jQuery可以很好地显示对象的集合,但是,如果仅返回一个对象,则它会为<option>元素显示未定义".

What I am having trouble with is how to use jQuery to display the results. The jQuery works fine to display a collection of objects, however if only one object is returned it displays "undefined" for the <option> element.

   function searchClients() {
      var uri = 'api/clients';
      var searchParam = $('#SearchTerm').val();
      $.getJSON(uri + '/' + searchParam)
        .done(function (data) {
          $('#selSearchResults').show(); // Displays hidden select element
          $('#selSearchResults').html(""); // Clear any pre-existing data in select element
          $.each(data, function (key, item) {
            $('#selSearchResults')
              .append('<option value="' + key + '">' + item.OrgName + ' - ' + item.ID + '</option>');
          })
        })
        .fail(function (jqXHR, textStatus, err) {
            $('#ErrorMessage').text('Error: ' + err);
        });
    }

如果.each函数为data接收到单个对象,是否返回undefined?如果是这样,一个人如何解释可能包含一个对象或一个对象的结果?我以为.each会解决这个问题.

If the .each function receives a single object for data will if return undefined? If this is so how does one account for a result that may contain a single object or just one? I thought the .each would account for this.

推荐答案

已解决!问题在于返回了单个对象,当发生这种情况时,each()函数会遍历该对象的属性.解决方案是将单个对象包装"到一个数组中,以便each()能够正确地进行迭代.我将此行添加到我的jQuery中...

Solved! The problem was that a single object was being returned and when this occurs the each() function iterates through the properties of the object. The solution was to "wrap" the single object into an array so each() would be able to iterate properly. I added this line to my jQuery ...

if (!jQuery.isArray(data)) data = [data]; // If a single object is returned, wrap it in an array

这里是整个功能.

function searchClients() {
   var uri = 'api/clients';
   var searchParam = $('#SearchTerm').val();
   $.getJSON(uri + '/' + searchParam)
     .done(function (data) {
       $('#selSearchResults').show(); // Displays hidden select element
       $('#selSearchResults').html(""); // Clear any pre-existing data in select element
       if (!jQuery.isArray(data)) data = [data]; // If a single object is returned, wrap it in an array
       $.each(data, function (key, item) {
         $('#selSearchResults')
           .append('<option value="' + key + '">' + item.OrgName + ' - ' + item.ID + '</option>');
       })
     })
     .fail(function (jqXHR, textStatus, err) {
         $('#ErrorMessage').text('Error: ' + err);
     });
}

我从以下 SO文章-仅包含JSON的JSON结果中找到了解决此问题的方法一项,然后查看来自 @CMS 的答案.

I found the solution to this problem from the following SO article - JSON result containing only one item and looking at the answer from @CMS.

这篇关于jQuery每个具有单个对象的循环都返回"undefined"(未定义).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆