Save Json in Javascript variable

html, javascript, json, variables

Solution

You would need a simple AJAX request to get the contents into a variable.

var xhReq = new XMLHttpRequest();
xhReq.open("GET", yourUrl, false);
xhReq.send(null);
var jsonObject = JSON.parse(xhReq.responseText);

Please note that AJAX is bound by same-origin-policy, in case that URL is different this will fail.

Problem

I'm studying javascript these days and I have question. I have variable that contain an url. I would like to save the content of the url pointed by my variable in another variable... The first variable is something like: ``` var Link = "http://mysite.com/json/public/search?q=variabile&k=&e=1"; ``` If I open the link I see something like: ``` { "count": 1, "e": 1, "k": null, "privateresult": 0, "q": "gabriel", "start": 0, "cards": [ { "__guid__": "cdf8ee96538c3811a6a118c72365a9ec", "company": false, "__title__": "Gabriel Butoeru", "__class__": "entity", "services": false, "__own__": false, "vanity_urls": [ "gabriel-butoeru" ] } ] } ``` How can I save the json content in another javascript variable?

Original source