I\'m new to javascript. I have a php file that lists all the files in a directory. I want to call that file and get the json array that it echos using only javascript. I know jq
I would say just use jquery as handling all different browsers for AJAX is a pain and I am sure you will use it in the long run for other things too.
If you really want to do this here is an example of a native js request:
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
you would use it like this:
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
var jsondata=eval("("+mygetrequest.responseText+")") //retrieve result as an JavaScript object
var rssentries=jsondata.items
}
else{
alert("An error has occured making the request")
}
}
}
mygetrequest.open("GET", "mypage.php", true)
mygetrequest.send(null)