首页 > 代码库 > js写ajax并解析json

js写ajax并解析json

function down(t){
 
 var req = createRequest();  //创建request
 req.open("GET","selectWordDate?date_time="+t);  //发送请求
 req.onreadystatechange = function(){
  callback(req);
 }
 req.send(null);
}

 

function createRequest()
{
 if(window.XMLHttpRequest)//ns
 {
  return new XMLHttpRequest();
 }else//IE
 {
  try{
      return new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
   return new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
 return null;
}

 

function callback(req){

 if(req.readyState == 4) {   if(req.status != 200){    return;   }     

 var json = req.responseText;  

    var o = JSON.parse(json);

  var datas=o.obj;  

 var fileName = datas.wordName;  

 var filePath = datas.fileUrl;  

    if(filePath!=""){

          window.location = "downloadFile?fileName=" + fileName + "&filePath=" + filePath;  

     }

    else{    

         alert("文件不存在");    return false;

       }      

    }

}

 

js写ajax并解析json