Tasker Exported App Javascriptlet Error - javascript

I have created a Task to get the JSON result from 500px using HTTP GET and then used Javascriptlet code:
var arr=JSON.parse(global('HTTPD'));
var name=arr.photos[0].name;`
To parse the JSON result, everything is working fine when run from the TASKER.
But after exporting it as an APP, it gives error on JSON.parse():
Uncaught TypeError: undefined is not a function
I tried using .js file also. Can't figure out what am I missing?

I would use javascriptlet in following way.
url = "your url here.";
http = newXMLHttpRequest();
http.open("GET",url,false);
json = eval("("+http.responeText+")");
yourvariablename = json.var[0].name;
Also i do an variable set before the javascriptelet task.
for example. #variable set to ...
then set that #variable to some value within json.

Related

Parse JSON data from a website with HTTP request with javascript

I've used package sniffer to get a specific endpoint, which gave me a direct JSON url. Now I want to get for all usernames from this JSON format.For eg. it goes like this:
{"node":{"id":"1428809588","username":"kafeterijabg","full_name":"\u041a\u0430\u0444\u0435\u0442\u0435\u0440\u0438\u0458\u0430\u00ae","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1428809588","expiring_at":1572193230,"has_pride_media":false,"latest_reel_media":1572095740,"seen":1572031349,"owner":{"__typename":"GraphUser","id":"1428809588","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"kafeterijabg"}}}}
I've tried sending requests and parsing it to console but I'm getting "undefined".
function Get(yourUrl){
var Httpreq = new XMLHttpRequest();
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);
return Httpreq.responseText;
}
var json_obj = JSON.parse(Get(MYURL));
console.log(json_obj.username);
I've never done anything similar before.
EDIT1:
It seems like I was using the wrong endpoint.But when I just try to get the output of the request without parsing it I'm getting only first two lines of It, even tho when I open the endpoint in browser I'm seeing all of it.
This is what I'm getting:
{"data":{"user":{"edge_follow":{"count":377,"page_info":{"has_next_page":false,"end_cursor":null},"edges":[]}}},"status":"ok"}
And here is what I expected to get.
{"data":{"user":{"edge_follow":{"count":377,"page_info":{"has_next_page":true,"end_cursor":"QVFBd2J1MzJKakRoNXI3bzVqR1hTYkRYRjJ0TGJrNjJxeFVnRmVsc0JwczZLR1d3VXloZDA4NXFiUFV6dXU4RlRfck93M0J5Z1ZINVdieEtwREVmWFZNaA=="},"edges":[{"node":{"id":"8752655712","username":"dragibravo","full_name":"Dragi Bravo \u00a9","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/5f53d36c1e430cbd5453478159c0a780/5E4CE6DD/t51.2885-19/s150x150/44656034_343135116439319_1392780490192191488_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"8752655712","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572123916,"seen":1572085057,"owner":{"__typename":"GraphUser","id":"8752655712","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/5f53d36c1e430cbd5453478159c0a780/5E4CE6DD/t51.2885-19/s150x150/44656034_343135116439319_1392780490192191488_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dragibravo"}}}},{"node":{"id":"21396562691","username":"dete_devedesetih","full_name":"detedevedesetih","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/88d2c2221aaa75ca2ea16ab32d0f1578/5E42CEE5/t51.2885-19/s150x150/69694071_2580132948676320_8436138521869156352_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"21396562691","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572123552,"seen":null,"owner":{"__typename":"GraphUser","id":"21396562691","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/88d2c2221aaa75ca2ea16ab32d0f1578/5E42CEE5/t51.2885-19/s150x150/69694071_2580132948676320_8436138521869156352_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dete_devedesetih"}}}},{"node":{"id":"14659853","username":"brittanya187","full_name":"Brittanya Razavi","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/f611badd19896aad1280c5743b4692d5/5E5907C2/t51.2885-19/s150x150/42002550_2204067873162229_8645854606632419328_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"14659853","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572121817,"seen":1572039263,"owner":{"__typename":"GraphUser","id":"14659853","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/f611badd19896aad1280c5743b4692d5/5E5907C2/t51.2885-19/s150x150/42002550_2204067873162229_8645854606632419328_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"brittanya187"}}}},{"node":{"id":"1465895116","username":"britnibrid","full_name":"neispavana lepotica","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ce45b2702afa309ca3c581dbd99ba68f/5E5A1E14/t51.2885-19/s150x150/44828900_327180521439297_1922646860288229376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1465895116","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"1465895116","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ce45b2702afa309ca3c581dbd99ba68f/5E5A1E14/t51.2885-19/s150x150/44828900_327180521439297_1922646860288229376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"britnibrid"}}}},{"node":{"id":"1428809588","username":"kafeterijabg","full_name":"\u041a\u0430\u0444\u0435\u0442\u0435\u0440\u0438\u0458\u0430\u00ae","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1428809588","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572107760,"seen":1572107760,"owner":{"__typename":"GraphUser","id":"1428809588","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/65f96409e2a6c5b9b72cda956aa0dfea/5E638B5F/t51.2885-19/s150x150/64620072_350924665805634_2914908438809018368_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"kafeterijabg"}}}},{"node":{"id":"13795435410","username":"jacobs_rs","full_name":"JacobsSrbija","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/4363dbdcf8e8a94bc65968dd4907ff60/5E64F5DB/t51.2885-19/s150x150/60911497_2333358360317600_4108961307611365376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"13795435410","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572081948,"seen":1572081948,"owner":{"__typename":"GraphUser","id":"13795435410","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/4363dbdcf8e8a94bc65968dd4907ff60/5E64F5DB/t51.2885-19/s150x150/60911497_2333358360317600_4108961307611365376_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"jacobs_rs"}}}},{"node":{"id":"6706638255","username":"apartment_a6_kopaonik","full_name":"Apartment A6 Kopaonik \u00ae","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/db9d49897114fb812add6b89f60198da/5E54AB81/t51.2885-19/s150x150/45378715_351931902032023_3560095462486704128_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"6706638255","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":1572023751,"owner":{"__typename":"GraphUser","id":"6706638255","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/db9d49897114fb812add6b89f60198da/5E54AB81/t51.2885-19/s150x150/45378715_351931902032023_3560095462486704128_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"apartment_a6_kopaonik"}}}},{"node":{"id":"21433171875","username":"dugestaze_svilajnac","full_name":"DUGE STAZE","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/d5e220b083bfb4432cd6f4dda3a81ba7/5E47E625/t51.2885-19/s150x150/71523182_308405693359685_2542825728215351296_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"21433171875","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"21433171875","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/d5e220b083bfb4432cd6f4dda3a81ba7/5E47E625/t51.2885-19/s150x150/71523182_308405693359685_2542825728215351296_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dugestaze_svilajnac"}}}},{"node":{"id":"280846376","username":"dekstroza","full_name":"dekstroza","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/838e15717ddeba3ff12d82657f3fa30e/5E5F3CCB/t51.2885-19/s150x150/70473686_396716634369751_3090030692306780160_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"280846376","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572101572,"seen":1572101572,"owner":{"__typename":"GraphUser","id":"280846376","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/838e15717ddeba3ff12d82657f3fa30e/5E5F3CCB/t51.2885-19/s150x150/70473686_396716634369751_3090030692306780160_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dekstroza"}}}},{"node":{"id":"6619361585","username":"ozbiljne_teme","full_name":"Ozbiljne Teme","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e56294e9abfe45f7c2ae2de54d208557/5E50CBDA/t51.2885-19/s150x150/53740023_372495643602512_1074420510437146624_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"6619361585","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572099187,"seen":1572099187,"owner":{"__typename":"GraphUser","id":"6619361585","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e56294e9abfe45f7c2ae2de54d208557/5E50CBDA/t51.2885-19/s150x150/53740023_372495643602512_1074420510437146624_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"ozbiljne_teme"}}}},{"node":{"id":"706629897","username":"mylifeaskitty","full_name":"Marija Zdravkovi\u0107","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ac6a53a0cb435d9900ee3984d153ae45/5E5C08E3/t51.2885-19/s150x150/66851574_2354137708132240_8606310035284295680_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"706629897","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"706629897","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ac6a53a0cb435d9900ee3984d153ae45/5E5C08E3/t51.2885-19/s150x150/66851574_2354137708132240_8606310035284295680_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"mylifeaskitty"}}}},{"node":{"id":"192815961","username":"gordongram","full_name":"Gordon Ramsay","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ca58f7dd68612bc05924e171092bcecf/5E579BFC/t51.2885-19/s150x150/26429265_1801356139896204_7973709430447407104_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"192815961","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"192815961","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ca58f7dd68612bc05924e171092bcecf/5E579BFC/t51.2885-19/s150x150/26429265_1801356139896204_7973709430447407104_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"gordongram"}}}},{"node":{"id":"19841914826","username":"vandometa_vlog","full_name":"","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d6c970339a04d31010618e05763e7c1/5E431AE5/t51.2885-19/s150x150/67943135_2416133855379280_5169818650630160384_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"19841914826","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"19841914826","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d6c970339a04d31010618e05763e7c1/5E431AE5/t51.2885-19/s150x150/67943135_2416133855379280_5169818650630160384_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"vandometa_vlog"}}}},{"node":{"id":"203227483","username":"tommycashworld","full_name":"TOMM\u00a5 \u20acA$H","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/130807ffdb11359677afba2220b62875/5E617C39/t51.2885-19/s150x150/73307916_1416703575159030_5557531713581613056_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"203227483","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"203227483","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/130807ffdb11359677afba2220b62875/5E617C39/t51.2885-19/s150x150/73307916_1416703575159030_5557531713581613056_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"tommycashworld"}}}},{"node":{"id":"4060156840","username":"fourtwenty","full_name":"Four Twenty","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d1136bc4a84cfbf432655ebd98e4070/5E5EE4DC/t51.2885-19/s150x150/70594066_1368719939948662_8046432933394776064_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"4060156840","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"4060156840","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2d1136bc4a84cfbf432655ebd98e4070/5E5EE4DC/t51.2885-19/s150x150/70594066_1368719939948662_8046432933394776064_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"fourtwenty"}}}},{"node":{"id":"6945680089","username":"lazybrain.ai","full_name":"LazyBrain Initiative","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/8b8324c443ffe3de2aa80641d2a9a8de/5E5BE2F3/t51.2885-19/s150x150/27879053_781778075349644_4543882190064189440_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"6945680089","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"6945680089","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/8b8324c443ffe3de2aa80641d2a9a8de/5E5BE2F3/t51.2885-19/s150x150/27879053_781778075349644_4543882190064189440_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"lazybrain.ai"}}}},{"node":{"id":"17100242394","username":"lifewith_crohn","full_name":"Mb.Crohn","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/916a9a84175036f50a6a598457759364/5E41A323/t51.2885-19/s150x150/66674988_2903343653224618_4794522445505626112_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"17100242394","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"17100242394","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/916a9a84175036f50a6a598457759364/5E41A323/t51.2885-19/s150x150/66674988_2903343653224618_4794522445505626112_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"lifewith_crohn"}}}},{"node":{"id":"5355293","username":"andy_mann","full_name":"Andy Mann","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e12200c75630dc7e1b2b814cb649d066/5E5E764B/t51.2885-19/s150x150/68862836_2425862274407548_3512337272991121408_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":true,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"5355293","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"5355293","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/e12200c75630dc7e1b2b814cb649d066/5E5E764B/t51.2885-19/s150x150/68862836_2425862274407548_3512337272991121408_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"andy_mann"}}}},{"node":{"id":"47457589","username":"istrahinja","full_name":"BJJ SANDMAN","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/a378f9ecc6df8cd0018088b6cfc5a5f5/5E4FC0A4/t51.2885-19/s150x150/19051700_1874715819469896_2141424226534424576_a.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"47457589","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"47457589","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/a378f9ecc6df8cd0018088b6cfc5a5f5/5E4FC0A4/t51.2885-19/s150x150/19051700_1874715819469896_2141424226534424576_a.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"istrahinja"}}}},{"node":{"id":"2158133293","username":"mpudja21","full_name":"\u041c\u0438\u0445\u0430\u0438\u043b\u043e \u041f\u0443\u0452\u0430","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/881e653f5d56d596133577f67a4a7e11/5E55C1F8/t51.2885-19/s150x150/74693418_1328190550698806_2099261116705144832_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"2158133293","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":1572031575,"owner":{"__typename":"GraphUser","id":"2158133293","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/881e653f5d56d596133577f67a4a7e11/5E55C1F8/t51.2885-19/s150x150/74693418_1328190550698806_2099261116705144832_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"mpudja21"}}}},{"node":{"id":"3998316794","username":"nature","full_name":"Nature","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2a4e907cbba6de380a178c95bc560376/5E49D099/t51.2885-19/s150x150/37130772_219216685405605_2651841279612157952_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"3998316794","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"3998316794","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2a4e907cbba6de380a178c95bc560376/5E49D099/t51.2885-19/s150x150/37130772_219216685405605_2651841279612157952_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"nature"}}}},{"node":{"id":"1808243250","username":"dobrotamilica","full_name":"\u13a0\u273a\u266d\u053b\u273a\u0442\u0394","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2b00e1cb3545393726a0bf1236e6c678/5E5DA2A1/t51.2885-19/s150x150/68908088_518976712264164_5633269725225025536_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1808243250","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":1572114311,"seen":1572114311,"owner":{"__typename":"GraphUser","id":"1808243250","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/2b00e1cb3545393726a0bf1236e6c678/5E5DA2A1/t51.2885-19/s150x150/68908088_518976712264164_5633269725225025536_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"dobrotamilica"}}}},{"node":{"id":"8738182557","username":"majceezmajcee","full_name":"Marija Palurovic","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ec6c203798b97791d8b0b6f1184f30d1/5E4FFACB/t51.2885-19/s150x150/57198888_414265232485760_3344820921243271168_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":true,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"8738182557","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"8738182557","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ec6c203798b97791d8b0b6f1184f30d1/5E4FFACB/t51.2885-19/s150x150/57198888_414265232485760_3344820921243271168_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"majceezmajcee"}}}},{"node":{"id":"1637485905","username":"niinamilanovic","full_name":"\u2664 N I N A \u2664","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ade9b86fc3985e70ab7c367b2cae4a26/5E50F5B7/t51.2885-19/s150x150/46354126_723371064697517_1754088811467374592_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","is_private":false,"is_verified":false,"followed_by_viewer":true,"requested_by_viewer":false,"reel":{"id":"1637485905","expiring_at":1572210555,"has_pride_media":false,"latest_reel_media":0,"seen":null,"owner":{"__typename":"GraphUser","id":"1637485905","profile_pic_url":"https://instagram.fbeg5-1.fna.fbcdn.net/vp/ade9b86fc3985e70ab7c367b2cae4a26/5E50F5B7/t51.2885-19/s150x150/46354126_723371064697517_1754088811467374592_n.jpg?_nc_ht=instagram.fbeg5-1.fna.fbcdn.net","username":"niinamilanovic"}}}}]}}},"status":"ok"}
EDIT 2:
Cookies were the problem, thats I was getting an empty JSON.
But I still dont know why
console.log(json_obj.node.username);
does not work.
It gives me this error:
Uncaught TypeError: Cannot read property 'username' of undefined
at :10:32
EDIT 3:
I've solved it with this code:
for (const node of json_obj.data.user.edge_follow.edges)
console.log(node.node.username);
in the json you posted above there is no tag username. To get to the user name you need to do json_obj.node.username
XMLHttpRequest is asynchronous, meaning that calling .open() and then .send() doesn't mean the request is complete after these calls.
Instead, you should set a function to the .onreadystatechange property like this:
var Httpreq = new XMLHttpRequest();
Httpreq.onreadystatechange = function() {
var json_obj = JSON.parse(this.responseText;
console.log(json_obj.username);
};
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);

Using a getJSON function to scrape webpage HTML I'm getting an error when trying to save the contents of the call to a variable

I'm using javascript/JQuery to scrape some HTML off of a webpage.
Here's the code I'm running:
var res = $.getJSON('http://api.allorigins.ml/get?url=https%3A//play.usaultimate.org/teams/events/Eventteam/%3FTeamId%3DDyfnwYxOk20EJ8z66rAnu8Vk%252fJ%252fHaEXYiIeBB7obkR0%253d&callback=?');
var data = res.responseJSON;
var htmlObject = $(data.contents);
typing this directly into the console works fine, however, when I attach this file as a script element onto an html file and open that file on my browser I'm getting a 'Uncaught TypeError: Cannot read property 'contents' of undefined' response and the data and htmlObject variables are undefined.
Any ideas where I'm going wrong or if I'm doing anything wrong at all?
The problem is the first line:
var res = $.getJSON('http://api.allorigins.ml/get?url=https%3A//play.usaultimate.org/teams/events/Eventteam/%3FTeamId%3DDyfnwYxOk20EJ8z66rAnu8Vk%252fJ%252fHaEXYiIeBB7obkR0%253d&callback=?');
You should see that by typing it into your console, you get undefined:
You need to do something with the data - here's how I rewrote your code to (hopefully) do what you want:
$.getJSON('http://api.allorigins.ml/get?url=https%3A//play.usaultimate.org/teams/events/Eventteam/%3FTeamId%3DDyfnwYxOk20EJ8z66rAnu8Vk%252fJ%252fHaEXYiIeBB7obkR0%253d&callback=?', function(data) {
var response = data.responseJSON;
var htmlObject = $(response.contents);
console.log(htmlObject);
});

"Unexpected end of JSON input" error on loading JSON file from local file system

Hi I'm trying to retrieve data from a title.JSON file into an index.html file using AJAX calls.Both these files resides in my local file system.I have created a new instance of the chrome and 've set its target property as "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"--disable-web-security --user-data-dir="c:/chromedev"(I'm aware that this is not a good practice. Just trying out a server less way).Below is my code
<h1><a id="headName" href="#">Name</a></h1>
<p onclick="spaLoad()">NameChange</p>
function spaLoad(){
var xhr = new XMLHttpRequest();
xhr.open('GET','title.json',true);
xhr.send();
xhr.onreadystatechange=function () {
//var obj=xhr.responseText;
var obj = JSON.parse(xhr.responseText);
console.log(obj);
console.log(xhr.readyState);
console.log(xhr.status);
console.log(xhr.statusText);
//document.getElementById('headName').innerHTML = obj;
document.getElementById('headName').innerHTML = obj.name;
}
}
title.json
{"name":"stackoverflow","age":"100"}
I get my h1 updated as "stackoverflow" through an ajax call along with the error
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse ()
at XMLHttpRequest.xhr.onreadystatechange
Here are my doubts:
1.I hope AJAX is for the communication between the client and the server. Though I have avoided using a server by modifying the browser settings, how did the ajax call work for me?Is it logical?
2.The JSON file contains the data as objects.Then why should I use JSON.parse(responseText)?(JSON.parse() is for converting the string from server into object for client I hope).If i directly give var obj=xhr.responseText;I get undefined.
3.readystate is changing to 4, but status is always 0.Why is it so?How could I resolve it.
Please give a brief explanation of how this server less ajax call works.I'm very new to ajax and is confused with this.
It is because readystate change fires multiple times and you expect it to fire once. You would need to check for readystate to be equal to 4 and the status to be 200 (or zero in your case since you are on the file protocol). Or use the onload and onerror events instead.
And if you still get a parsing error than you need to debug what is being returned.
//see what is returned
console.log(xhr.responseText)
//Look to see if you have hidden characters in the file.
console.log(escape(xhr.responseText))
Parsing errors occur either the file you are loading returns something else like an error page OR it has special hidden characters that cause the parser to choke.

Is it possible to modify a variable in Postman

I'm using Postman and I am trying to chain together some requests. There is an identity string that is generated in my first request that I would like to use in my second request e.g. similar to searching for a product, and then adding that product to basket.
Now I've been able to pull the value from the first request and I can pick that up in the second request. The problem is that the identity string has an ampersand in it. When I post the second request, it throws an error because the ampersand has not been escaped in the string. I would like to replace the ampersand in the variable with "&" but I can't get this to work.
I'm new to JavaScript so I imagine this is where the problem is. In Postman I have:
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
postman.setEnvironmentVariable("ItineraryId", jsonObject.ItineraryId);
ItineraryId.replace("&","&");
This returns "There was an error in evaluating the test script: ItineraryId is not defined". So I tried:
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
var oldId = postman.setEnvironmentVariable("ItineraryId", jsonObject.ItineraryId);
oldId.replace("&","&");
And got "There was an error in evaluating the test script: Cannot read property 'replace' of undefined"
Thanks in advance.
Figured it out!
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
var itineraryId = jsonObject.ItineraryId;
itineraryId = itineraryId.replace("&","&");
postman.setEnvironmentVariable("ItineraryId", itineraryId);
You should user pm.collectionVariables.set like in example below :
pm.collectionVariables.set("userId", pm.response.headers.get('Location').replace(pm.environment.get("api_url") + "/api/users/", ""));
In this example after POST on /api/users in response header : Location we receive link to new resource. You can use other variables like : pm.environment.get("api_url")

Twitter Bootstrap Typeahead With Static Data

I need to use typeahead for very large data. Generation of data consumes 4-5 seconds. So I cannot make Ajax calls each time. I download and cache the data upon user request. My code is the following:
$("#build-package-list-btn").click(function(){
$.get**JSON**("/packages", function(data){
// data is an array
$("#typeahead-packages").typeahead({source:data});
console.log(data == null); // returns false
});
})
It gives no error but whenever I try to write to typeahead text box, it gives me the following error:
Uncaught TypeError: Cannot call method 'toLowerCase' of null bootstrap.js:1644
Typeahead.matcher bootstrap.js:1644
(anonymous function) bootstrap.js:1631
e.extend.grep jquery-1.7.2.min.js:2
Typeahead.lookup bootstrap.js:1630
Typeahead.keyup bootstrap.js:1738
e.extend.proxy.g jquery-1.7.2.min.js:2
f.event.dispatch jquery-1.7.2.min.js:3
f.event.add.h.handle.i jquery-1.7.2.min.js:3
My typeahead is like this (JADE)
input#typeahead-packages(type="text",data-provide="typeahead")
Also in the Chrome Console I tried:
$("#typeahead-packages").typeahead({source:["abcdef","abcddd","abcccc"]});
But typeahead does not give an error but it also does not work. I cannot find what am I doing wrong. I am using the 2.0.4 bootstrap.
EDIT: I changed it to getJSON from get it did not help. However when I construct data like this, it is working:
data = [new String((data[0])), new String((data[5]))];
Based on your update, the problem comes from the JSON.
jQuery.getJSON()
Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.
(source)
You should concentrate on the source of the items. be sure that this is an array of strings. Your error suggest data is a string.
You can use the Chrome Console (or any of your choice), under the Network tab, and spy on the actual results of your AJAX request. JSONLint is an interesting tool to validate your result.
Then your next problem will be to update the source of an already initialized typeahead. You can use the following code :
var $myTypeahead = $('#myTypeahead');
var typeaheadObj = $myTypeahead.data('typeahead');
if(typeaheadObj) typeaheadObj.source = ["abc", "cde"];
Example (jsfiddle)

Categories

Resources