Я использую метод Ajax POST для получения междоменных результатов. Результаты возвращаются, когда я выполняю:

var results = JSON.stringify(data); console.log(results); 

Я получаю следующее:

 {"ERR":null,"RSP":{"FLDR":{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505397375000","NM":"Documents","DESC":"Repository Root Folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":null,"FLDRS":[{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506442678000","NM":"Folder 1","DESC":"Level 1 Test Folder 1","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null},{"ID":"5","TJT":"FULL","ACTV":"true","BO":"1505397610000","LU":"1506442685000","NM":"Folder 4","DESC":"folder 4","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"8","TJT":"FULL","ACTV":"true","BO":"1506444461000","LU":"1506444461000","NM":"Test Folder","DESC":"Please delete this folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"5","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null},{"ID":"6","TJT":"FULL","ACTV":"true","BO":"1505405095000","LU":"1506442367000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"7","TJT":"FULL","ACTV":"true","BO":"1505405109000","LU":"1506442672000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"6","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null}],"DOCS":[{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505400744000","TTL":"Test Document 1","DESC":"Document 1","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":null,"VAL1":null,"VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":null,"VAL7":null,"VAL8":null,"VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"1","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null},{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506441814000","TTL":"Test Employee Document 1","DESC":"Document 2","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":"General Information","VAL1":"Pete Morgan","VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":"39.0","VAL7":null,"VAL8":"1505397375491","VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"2","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null}],"PROPS":null}}} 

Проблема в том, что когда я выполняю эту функцию, я получаю следующую ошибку:

TypeError: элемент равен нулю

 function getSubfolders(acctId,repoId,fldrId) { $.ajax({ type: 'POST', url: 'http://52.36.x.xxx/sdws/appv1/accts/'   storage.get('LoginID')   '/repos/'   repoId   '/fldrs/'   fldrId, crossDomain: true, xhrFields: { withCredentials: true }, data: '{"CASCD":"true"}', dataType: "json", contentType: "application/json; charset=utf-8", success: function(data) { $.each(data.RSP.FLDR, function(index, element) { console.log(element.NM); }); }, error: function(e) { console.log(e); } }); } 

Я не понимаю, чего мне здесь не хватает. Кажется, что каждое утверждение зацикливается, но я получаю «нулевые» элементы. Исходя из объекта, который я возвращаю, кажется, что я должен получить «Папка 1», «Папка 4» и т. Д. Есть идеи?

Переменные, которые вы назвали «index» и «element», на самом деле являются парами ключ и значение в data.RSP.FLDR — ваши «нулевые» результаты — потому что вы пытаетесь вывести element.NM , когда вы хотите получить element.NM значение на самом деле внутри самого element .

Тем не менее, код в вашем вопросе будет искать только один путь в данных, data.RSP.FLDR — если это то, что вам нужно, то ваш код может быть следующим:

 console.log(data.RSP.FLDR.NM) 

Если вместо этого вы пытаетесь найти все значения NM в дереве данных, вам нужно выполнить рекурсивный поиск по дереву, а не по одному циклу в each . Это работает, например:

 var data = {"ERR":null,"RSP":{"FLDR":{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505397375000","NM":"Documents","DESC":"Repository Root Folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":null,"FLDRS":[{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506442678000","NM":"Folder 1","DESC":"Level 1 Test Folder 1","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":null,"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null},{"ID":"5","TJT":"FULL","ACTV":"true","BO":"1505397610000","LU":"1506442685000","NM":"Folder 4","DESC":"folder 4","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"8","TJT":"FULL","ACTV":"true","BO":"1506444461000","LU":"1506444461000","NM":"Test Folder","DESC":"Please delete this folder","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"5","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null},{"ID":"6","TJT":"FULL","ACTV":"true","BO":"1505405095000","LU":"1506442367000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"FLDRS":[{"ID":"7","TJT":"FULL","ACTV":"true","BO":"1505405109000","LU":"1506442672000","NM":"Folder 5","DESC":"test folder object","ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"USR":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"6","TJT":"ID"},"FLDRS":null,"DOCS":null,"PROPS":null}],"DOCS":null,"PROPS":null}],"DOCS":[{"ID":"1","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1505400744000","TTL":"Test Document 1","DESC":"Document 1","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":null,"VAL1":null,"VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":null,"VAL7":null,"VAL8":null,"VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"1","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null},{"ID":"2","TJT":"FULL","ACTV":"true","BO":"1505397375000","LU":"1506441814000","TTL":"Test Employee Document 1","DESC":"Document 2","FLNM":null,"SZ":"0","FL":"false","AUDIT":"false","ADDR":"0","ENCD":"true","INIV":null,"KEY":null,"CMP":"false","CMPSZ":"0","VAL0":"General Information","VAL1":"Pete Morgan","VAL2":null,"VAL3":null,"VAL4":null,"VAL5":null,"VAL6":"39.0","VAL7":null,"VAL8":"1505397375491","VAL9":null,"ACCT":{"ID":"1","TJT":"ID"},"REPO":{"ID":"1","TJT":"ID"},"FLDR":{"ID":"1","TJT":"ID"},"DOCTP":{"ID":"2","TJT":"ID"},"USR":{"ID":"3","TJT":"ID"},"PROPS":null}],"PROPS":null}}} var searchTree = function(obj) { if (obj {amp}amp;{amp}amp; typeof(obj) === 'object') { $.each(obj, function(k, v) { if (k === 'NM') { console.log(v) // found one } searchTree(v); // recurse to child objects }) } } searchTree(data); 
 {amp}lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"{amp}gt;{amp}lt;/script{amp}gt;