Javascript:从JSON获取数据

时间:2012-01-26 19:46:39

标签: javascript jquery ajax

基础知识:我有一个HTML表格。我想在不重新加载页面的情况下向表中添加行。我创建了一个servlet,只要单击“添加”按钮,就会返回我想要添加到表中的值。

问题:我可以动态地向表中添加行。问题是从servlet中获取数据.servlet非常简单。发送三个值,它返回一个带有新行值的JSON。我已经测试过,它运行得很好。问题是我用来保存值的变量不会超出我在$ .getJSON()中设置的函数。我没有很多javascript的经验,所以我不完全确定变量声明的优先级,但有一件事是肯定的,一旦内部函数完成,我的变量就不会保持它们的值。任何见解都将不胜感激。

function addPerson()
{
    var e = document.getElementById("newperson");
    var personid = e.options[e.selectedIndex].value;
    var roleid = e.options[e.selectedIndex].value;
    var r = document.getElementById("newrole");
    var roleid = r.options[r.selectedIndex].value;
    var o = document.getElementById("thisorganizationid");
    var orgid = o.options[o.selectedIndex].value;

    var personName = "";
    var orgName = "";
    var roleName = "";

    $.getJSON("GetPersonRole",{
        personid: personid,
        roleid: roleid,
        orgid: orgid,
      }, function(json) {
          personName = json.Person;
          orgName = json.Organization;
          roleName = json.Role;

          alert('person: ' + personName + '  role: ' + roleName); //They all display properly here
      });

    var table = document.getElementById("PersonTable");
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0);
    cell1.innerHTML = orgName;

    var cell2 = row.insertCell(1);
    cell2.innerHTML = personName;

    var cell3 = row.insertCell(2);
    cell3.innerHTML = roleName;

    var cell4 = row.insertCell(3);
    cell4.innerHTML = "adding";

    document.getElementById('addingPerson').innerHTML = addString;
}

4 个答案:

答案 0 :(得分:3)

getJSON()是一个异步函数。如果您不确定这意味着什么,我建议您阅读有关异步JS编程的教程。

您问题的简单答案是:您正在尝试在getJSON()方法完成之前将变量值分配给DOM元素。这是一个快速修复(请参阅注释以获得进一步说明):

...

$.getJSON("GetPersonRole",{
        personid: personid,
        roleid: roleid,
        orgid: orgid,
      }, function(json) { //Executes when the asynchronous call is complete
          personName = json.Person;
          orgName = json.Organization;
          roleName = json.Role;

          UpdateMyPage(personName, orgName, roleName);
});

//Most likely will execute before the asynchronous call is complete
alert("HERE");

function UpdateMyPage(personName, orgName, roleName) {
    var table = document.getElementById("PersonTable");
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0);
    cell1.innerHTML = orgName;

    var cell2 = row.insertCell(1);
    cell2.innerHTML = personName;

    var cell3 = row.insertCell(2);
    cell3.innerHTML = roleName;

    var cell4 = row.insertCell(3);
    cell4.innerHTML = "adding";

    document.getElementById('addingPerson').innerHTML = addString;   
}

答案 1 :(得分:1)

您的回调函数:

    $.getJSON("GetPersonRole",{
        personid: personid,
        roleid: roleid,
        orgid: orgid,
      }, function(json) {
          personName = json.Person;
          orgName = json.Organization;
          roleName = json.Role;

          alert('person: ' + personName + '  role: ' + roleName); //They all display properly here
 });

返回AJAX调用时执行。这意味着在将变量添加到表之前,不会使用新值设置变量。

您的代码应该是这样的:

function addPerson()
{
    var e = document.getElementById("newperson");
    var personid = e.options[e.selectedIndex].value;
    var roleid = e.options[e.selectedIndex].value;
    var r = document.getElementById("newrole");
    var roleid = r.options[r.selectedIndex].value;
    var o = document.getElementById("thisorganizationid");
    var orgid = o.options[o.selectedIndex].value;

    var personName = "";
    var orgName = "";
    var roleName = "";

    $.getJSON("GetPersonRole",{
        personid: personid,
        roleid: roleid,
        orgid: orgid,
      }, function(json) {
          personName = json.Person;
          orgName = json.Organization;
          roleName = json.Role;

          alert('person: ' + personName + '  role: ' + roleName); //They all display properly here

          var table = document.getElementById("PersonTable");
          var rowCount = table.rows.length;
          var row = table.insertRow(rowCount);

          var cell1 = row.insertCell(0);
          cell1.innerHTML = orgName;

          var cell2 = row.insertCell(1);
          cell2.innerHTML = personName;

          var cell3 = row.insertCell(2);
          cell3.innerHTML = roleName;

          var cell4 = row.insertCell(3);
          cell4.innerHTML = "adding";

          document.getElementById('addingPerson').innerHTML = addString;
     });
}

答案 2 :(得分:0)

看起来在服务器返回值之前添加了行。我会make和addRow()函数并在成功时调用它:

function addRow(json){
    // include all code after the getJSON method above...
    var cell1 = row.insertCell(0);
    cell1.innerHTML = json.Organization;

    var cell2 = row.insertCell(1);
    cell2.innerHTML = json.Person;

    // etc...
}

然后你需要这样做:

$.getJSON("GetPersonRole",{
    personid: personid,
    roleid: roleid,
    orgid: orgid,
  }, addRow);

答案 3 :(得分:-5)

您的变量是在函数范围内定义的。如果您在全球范围内定义它们,就像这样,您应该能够在其他地方访问它们:

var personName = "";
var orgName = "";
var roleName = "";
function addPerson()
    {
        ...

etc...