Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  59] [ 1]  / answers: 1 / hits: 49302  / 12 Years ago, wed, june 20, 2012, 12:00:00

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?


More From » html

 Answers
27

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.


[#84785] Tuesday, June 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alli

Total Points: 409
Total Questions: 101
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
alli questions
Sat, Apr 23, 22, 00:00, 2 Years ago
Mon, May 18, 20, 00:00, 4 Years ago
Tue, Mar 24, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;