Json data to index.php question

Hello all
trying to pick up some jSon.
i have a reg index.php file.
I have this js that is called in the index file

var request;
	if(window.XMLHttpRequest){
		request=new XMLHttpRequest();
	}else{
	request=new ActiveXObject("Microsoft.XMLHTTP");
	}
	request.open('GET', '/js/data.json');
	request.onreadystatehchange = function(){
		if((request.status=200) &&
			(request.readyState===4)){
			

	var output =" ";
	for(var i =0; i<=info.link.length-1;i++){
		for(key in info.links[i]){
			if(info.links[i].hasOwnProperty(key)){
				output += '<li>' + '<a href ="' +info.links[key] + '">' + key + '</a>' +  '</li>';
			}
		}	
	}

var update = document.getElementById('links');
update.innerHTML = output;
		}
	}
request.send();

and this in the data.json file

var info={
	"full_name": "jane doe",
	"title": "web author",
	"links":[
		{"blog":"http://marketthis.com"},
		{"fb":"http://marketthis1.com"},
		{"yt":"http://marketthis2.com"},
		{"web":"http://marketthis3.com"},
		{"twitt":"http://marketthis4.com"}
	]
};

And lastly this bit in the index.php

<ul id="links" class="links">


</ul>

am doing this on a localhost. in chrome i get an error, > GET http://localhost/js/data.json 404 (Not Found)
but i don’t get a an error in FF. Although it doesn’t display the data there either.
So was hoping you could tell me what i am doing wrong.
thx
D

quick addendum. saw and corrected some of my js errors. like the typos



var request;
	if(window.XMLHttpRequest){
		request=new XMLHttpRequest();
	}else{
	request=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	
	
	request.open('GET', '/jSon/data.json');
	request.onreadystatechange = function(){
		if((request.status === 200) &&
			(request.readyState === 4)){
			
			var output="";
			for(var i=0; i<=info.links.length;i){
				for(key in info.links[i]){
					if(info.links[i].hasOwnProperty(key)){
					output += '<li>'+'<a href="' + info.links[i][key] + '">' + key + '</a>';
					'</li>';
					}
				}
			}
		var update = document.getElementById('links');
		update.innerHTML = output;
		}
	}
request.send();

i think your JSON-file should not contain any variable declarations
there should be “pure” JSON:

{
   "full_name": "jane doe",
   "title": "web author",
   "links":[
	{"blog":"http://marketthis.com"},
	{"fb":"http://marketthis1.com"},
	{"yt":"http://marketthis2.com"},
	{"web":"http://marketthis3.com"},
	{"twitt":"http://marketthis4.com"}
    ]
}

and you have to parse it after loading:

if (response.readyState == 4 && response.status == 200) {
    var info = JSON.parse(response.responseText);
    // do something with info
}

will give it a try thank you
D

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.