var url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/party";
var token = "XXXX";
var query = "7707083893";
var options = {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Token " + token
},
body: JSON.stringify({query: query})
}
fetch(url, options)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log("error", error));
{"suggestions":[{"value":"ПАО СБЕРБАНК","unrestricted_value":"ПАО СБЕРБАНК","data":{"kpp":"773601001","capital":null,"management":{"name":"Греф Герман Оскарович","post":"ПРЕЗИДЕНТ, ПРЕДСЕДАТЕЛЬ ПРАВЛЕНИЯ","disqualified":null},"founders":null,"managers":null,"predecessors":null,"successors":null,"branch_type":"MAIN","branch_count":86,"source":null,"qc":null,"hid":"588a141bc5e17cbc976ec2d0d54149af49d5a4ca16e26ed2effafdf06841d645","type":"LEGAL","state":{"status":"ACTIVE","code":null,"actuality_date":1653868800000,"registration_date":677376000000,"liquidation_date":null},"opf":{"type":"2014","code":"12247","full":"Публичное акционерное общество","short":"ПАО"},"name":{"full_with_opf":"ПУБЛИЧНОЕ АКЦИОНЕРНОЕ ОБЩЕСТВО \"СБЕРБАНК РОССИИ\"","short_with_opf":"ПАО СБЕРБАНК","latin":null,"full":"СБЕРБАНК РОССИИ","short":"СБЕРБАНК"},"inn":"7707083893","ogrn":"1027700132195","okpo":"00032537","okato":"45293554000","oktmo":"45397000000","okogu":"4100104","okfs":"41","okved":"64.19","okveds":null,"authorities":null,"documents":null,"licenses":null,"finance":null,"address":{"value":"г Москва, ул Вавилова, д 19","unrestricted_value":"117312, г Москва, Академический р-н, ул Вавилова, д 19","data":{"postal_code":"117312","country":"Россия","country_iso_code":"RU","federal_district":"Центральный","region_fias_id":"0c5b2444-70a0-4932-980c-b4dc0d3f02b5","region_kladr_id":"7700000000000","region_iso_code":"RU-MOW","region_with_type":"г Москва","region_type":"г","region_type_full":"город","region":"Москва","area_fias_id":null,"area_kladr_id":null,"area_with_type":null,"area_type":null,"area_type_full":null,"area":null,"city_fias_id":"0c5b2444-70a0-4932-980c-b4dc0d3f02b5","city_kladr_id":"7700000000000","city_with_type":"г Москва","city_type":"г","city_type_full":"город","city":"Москва","city_area":"Юго-западный","city_district_fias_id":null,"city_district_kladr_id":null,"city_district_with_type":"Академический р-н","city_district_type":"р-н","city_district_type_full":"район","city_district":"Академический","settlement_fias_id":null,"settlement_kladr_id":null,"settlement_with_type":null,"settlement_type":null,"settlement_type_full":null,"settlement":null,"street_fias_id":"25f8f29b-b110-40ab-a48e-9c72f5fb4331","street_kladr_id":"77000000000092400","street_with_type":"ул Вавилова","street_type":"ул","street_type_full":"улица","street":"Вавилова","stead_fias_id":null,"stead_cadnum":null,"stead_type":null,"stead_type_full":null,"stead":null,"house_fias_id":"93409d8c-d8d4-4491-838f-f9aa1678b5e6","house_kladr_id":"7700000000009240170","house_cadnum":"77:06:0002008:1036","house_type":"д","house_type_full":"дом","house":"19","block_type":null,"block_type_full":null,"block":null,"entrance":null,"floor":null,"flat_fias_id":null,"flat_cadnum":null,"flat_type":null,"flat_type_full":null,"flat":null,"flat_area":null,"square_meter_price":null,"flat_price":null,"postal_box":null,"fias_id":"93409d8c-d8d4-4491-838f-f9aa1678b5e6","fias_code":"77000000000000009240170","fias_level":"8","fias_actuality_state":"0","kladr_id":"7700000000009240170","geoname_id":"524901","capital_marker":"0","okato":"45293554000","oktmo":"45397000","tax_office":"7736","tax_office_legal":"7736","timezone":"UTC+3","geo_lat":"55.7001865","geo_lon":"37.5802234","beltway_hit":"IN_MKAD","beltway_distance":null,"metro":[{"name":"Ленинский проспект","line":"Калужско-Рижская","distance":0.8},{"name":"Площадь Гагарина","line":"МЦК","distance":0.8},{"name":"Академическая","line":"Калужско-Рижская","distance":1.5}],"divisions":{"administrative":{"area":null,"city":{"fias_id":"0c5b2444-70a0-4932-980c-b4dc0d3f02b5","kladr_id":"7700000000000","type":"г","type_full":"город","name":"Москва","name_with_type":"г Москва"},"settlement":null,"city_district":{"fias_id":null,"kladr_id":null,"type":"р-н","type_full":"район","name":"Академический","name_with_type":"Академический р-н"}},"municipal":null},"qc_geo":"0","qc_complete":null,"qc_house":null,"history_values":null,"unparsed_parts":null,"source":"117312, ГОРОД МОСКВА, УЛ. ВАВИЛОВА, Д.19","qc":"0"}},"phones":null,"emails":null,"ogrn_date":1029456000000,"okved_type":"2014","employee_count":null}},{"value":"БАЙКАЛЬСКИЙ БАНК ПАО СБЕРБАНК","unrestricted_value":"БАЙКАЛЬСКИЙ БАНК ПАО СБЕРБАНК","data":{"kpp":"380843001","capital":null,"management":null,"founders":null,"managers":null,"predecessors":null,"successors":null,"branch_type":"BRANCH","branch_count":0,"source":null,"qc":null,"hid":"259fdc9a81120194c8534e3394049da8d2f13666387db9aff615bf9acbc2bbf9","type":"LEGAL","state":{"status":"ACTIVE","code":null,"actuality_date":1648598400000,"registration_date":null,"liquidation_date":null},"opf":{"type":"2014","code":"30002","full":"Филиал юридического лица","short":"Филиал"},"name":{"full_with_opf":"БАЙКАЛЬСКИЙ БАНК ПАО СБЕРБАНК","short_with_opf":"БАЙКАЛЬСКИЙ БАНК ПАО СБЕРБАНК","latin":null,"full":"БАЙКАЛЬСКИЙ БАНК ПАО СБЕРБАНК","short":"БАЙКАЛЬСКИЙ БАНК ПАО СБЕРБАНК"},"inn":"7707083893","ogrn":"1027700132195","okpo":null,"okato":null,"oktmo":null,"okogu":null,"okfs":null,"okved":null,"okveds":null,"authorities":null,"documents":null,"licenses":null,"finance":null,"address":{"value":"г Иркутск, ул Нижняя Набережная, д 10","unrestricted_value":"664011, Иркутская обл, г Иркутск, Кировский р-н, ул Нижняя Набережная, д 10","data":{"postal_code":"664011","country":"Россия","country_iso_code":"RU","federal_district":"Сибирский","region_fias_id":"6466c988-7ce3-45e5-8b97-90ae16cb1249","region_kladr_id":"3800000000000","region_iso_code":"RU-IRK","region_with_type":"Иркутская обл","region_type":"обл","region_type_full":"область","region":"Иркутская","area_fias_id":null,"area_kladr_id":null,"area_with_type":null,"area_type":null,"area_type_full":null,"area":null,"city_fias_id":"8eeed222-72e7-47c3-ab3a-9a553c31cf72","city_kladr_id":"3800000300000","city_with_type":"г Иркутск","city_type":"г","city_type_full":"город","city":"Иркутск","city_area":null,"city_district_fias_id":null,"city_district_kladr_id":null,"city_district_with_type":"Кировский р-н","city_district_type":"р-н","city_district_type_full":"район","city_district":"Кировский","settlement_fias_id":null,"settlement_kladr_id":null,"settlement_with_type":null,"settlement_type":null,"settlement_type_full":null,"settlement":null,"street_fias_id":"241f2f43-5a67-4d79-b440-fda4a700ad45","street_kladr_id":"38000003000038000","street_with_type":"ул Нижняя Набережная","street_type":"ул","street_type_full":"улица","street":"Нижняя Набережная","stead_fias_id":null,"stead_cadnum":null,"stead_type":null,"stead_type_full":null,"stead":null,"house_fias_id":"9332c1e4-555c-478c-90cd-08a3e4454ef9","house_kladr_id":"3800000300003800001","house_cadnum":null,"house_type":"д","house_type_full":"дом","house":"10","block_type":null,"block_type_full":null,"block":null,"entrance":null,"floor":null,"flat_fias_id":null,"flat_cadnum":null,"flat_type":null,"flat_type_full":null,"flat":null,"flat_area":null,"square_meter_price":null,"flat_price":null,"postal_box":null,"fias_id":"9332c1e4-555c-478c-90cd-08a3e4454ef9","fias_code":"38000003000000003800001","fias_level":"8","fias_actuality_state":"0","kladr_id":"3800000300003800001","geoname_id":"2023469","capital_marker":"2","okato":"25401365000","oktmo":"25701000001","tax_office":"3808","tax_office_legal":"3808","timezone":"UTC+8","geo_lat":"52.2928747","geo_lon":"104.2875056","beltway_hit":null,"beltway_distance":null,"metro":null,"divisions":{"administrative":{"area":null,"city":{"fias_id":"8eeed222-72e7-47c3-ab3a-9a553c31cf72","kladr_id":"3800000300000","type":"г","type_full":"город","name":"Иркутск","name_with_type":"г Иркутск"},"settlement":null,"city_district":{"fias_id":null,"kladr_id":null,"type":"р-н","type_full":"район","name":"Кировский","name_with_type":"Кировский р-н"}},"municipal":null},"qc_geo":"0","qc_complete":null,"qc_house":null,"history_values":null,"unparsed_parts":null,"source":"664011, ОБЛАСТЬ ИРКУТСКАЯ, ГОРОД ИРКУТСК, УЛИЦА НИЖНЯЯ НАБЕРЕЖНАЯ, 10","qc":"0"}},"phones":null,"emails":null,"ogrn_date":null,"okved_type":null,"employee_count":null}},{"value":"ДАЛЬНЕВОСТОЧНЫЙ БАНК ПАО СБЕРБАНК","unrestricted_value":"ДАЛЬНЕВОСТОЧНЫЙ БАНК ПАО СБЕРБАНК","data":{"kpp":"272143001","capital":null,"management":null,"founders":null,"managers":null,"predecessors":null,"successors":null,"branch_type":"BRANCH","branch_count":0,"source":null,"qc":null,"hid":"1e7c767641329c40a565ec1c3a64603049f4990777fac5300bc6bf1020b9de2c","type":"LEGAL","state":{"status":"ACTIVE","code":null,"actuality_date":1648598400000,"registration_date":null,"liquidation_date":null},"opf":{"type":"2014","code":"30002","full":"Филиал юридического лица","short":"Филиал"},"name":{"full_with_opf":"ДАЛЬНЕВОСТОЧНЫЙ БАНК ПАО СБЕРБАНК","short_with_opf":"ДАЛЬНЕВОСТОЧНЫЙ БАНК ПАО СБЕРБАНК","latin":null,"full":"ДАЛЬНЕВОСТОЧНЫЙ БАНК ПАО СБЕРБАНК","short":"ДАЛЬНЕВОСТОЧНЫЙ БАНК ПАО СБЕРБАНК"},"inn":"7707083893","ogrn":"1027700132195","okpo":null,"okato":null,"oktmo":null,"okogu":null,"okfs":null,"okved":null,"okveds":null,"authorities":null,"documents":null,"licenses":null,"finance":null,"address":{"value":"г Хабаровск, ул Гамарника, д 12","unrestricted_value":"680020, Хабаровский край, г Хабаровск, ул Гамарника, д 12","data":{"postal_code":"680020","country":"Россия","country_iso_code":"RU","federal_district":"Дальневосточный","region_fias_id":"7d468b39-1afa-41ec-8c4f-97a8603cb3d4","region_kladr_id":"2700000000000","region_iso_code":"RU-KHA","region_with_type":"Хабаровский край","region_type":"край","region_type_full":"край","region":"Хабаровский","area_fias_id":null,"area_kladr_id":null,"area_with_type":null,"area_type":null,"area_type_full":null,"area":null,"city_fias_id":"a4859da8-9977-4b62-8436-4e1b98c5d13f","city_kladr_id":"2700000100000","city_with_type":"г .......
Uncaught ReferenceError: result is not definedТак а куда ты вставил?
fetch(url, options)
.then(response => response.text())
.then(result => console.log(result))
.then(result => console.log(JSON.parse(result).suggestions.data))
.catch(error => console.log("error", error));
application/json
, то JSON.parse
вам делать абсолютно не нужно. Ну а далее уже нужно правильно обратиться к объекту и видеть, что где-то есть массивы, а где-то есть объекты. $('body').on('click', '.btn', function(){
var url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/findById/party";
var token = "416480421932499786626f808ff1570e1130a497";
var query = "7707083893";
var options = {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Token " + token
},
body: JSON.stringify({query: query})
}
fetch(url, options)
.then(response => response.text())
//.then(result => console.log(JSON.parse(result).suggestions[0].data.management.name))
//.then(result => JSON.parse(result).suggestions[0].data.management.name)
.then((result) => {
aaa = JSON.parse(result).suggestions[0].data.management.name;
});
console.log('111'+aaa);
});
сначала выдает просто 111Естественно, так как фетч не выполняется мгновенно, и скрипт не ждет завершения фетч, а сразу переходит к логу. На второй раз данные уже есть в переменной (так как фетч закончил работу и засетил переменную). Используй async/await если нужно получить данные вне фетча.