0

password.json由组成

{    
    "passwd":"pavankumar",
     "name":"pavan"

};

在 Mozilla 和 Chrome 中尝试使用分号和不使用分号

function ajax_post(){

  var hr=  new XMLHttpRequest();

  hr.open("GET", "password.json", true);

  hr.setRequestHeader("Content-type", "application/json",true);

  hr.onreadystatechange = function(){

    if(hr.readyState == 4 && hr.status == 200){

       var data = JSON.parse(hr.responseText);
       alert(data);   // At this stage the json file is displayed. correctly..
       var status = document.getElementById("status");
       status.innerHTML= data.name;     //returning the undefined value....     
    }
  }

  hr.send(null);
}
4

1 回答 1

0

hr.responseText将是一个表示对象的 JSON 编码的字符串,所以你想要JSON.stringify它;这会将其转换为单个字符串的 JSON 表示。直接点就JSON.parse行了。

于 2012-10-24T13:11:26.390 回答