Javascript 使用 JSON 解析 xmlhttp.responseText 以填充文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12359047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:48:53  来源:igfitidea点击:

Using JSON to parse xmlhttp.responseText for populating textboxes

javascriptajaxjson

提问by programm3r

How can JSON be used to parse xmlhttp.responseText? I can't seem to get textboxes populated using the parsed data. I tried using .value and .innerHTML with the dot notation with b.first and b.second used with json_encode from the loadTextBox.php file (see below), but the textboxes won't populate.

如何使用 JSON 解析 xmlhttp.responseText?我似乎无法使用解析的数据填充文本框。我尝试使用带有点符号的 .value 和 .innerHTML 以及 b.first 和 b.second 与 loadTextBox.php 文件中的 json_encode 一起使用(见下文),但文本框不会填充。

Main page code:

主页面代码:

function loadDoc()
{
   var xmlhttp;

   // code for IE7+, Firefox, Chrome, Opera, Safari
   if (window.XMLHttpRequest)
   {
      xmlhttp=new XMLHttpRequest();
   }
   //code for IE6, IE5
   else
   {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.onreadystatechange=function()
   {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
         var doc = window.document.createElement("doc");
         var a = xmlhttp.responseText;
         var b = JSON.parse(a);
         document.getElementById("textbox").innerHTML=b.first;
         document.getElementById("textbox2").innerHTML=b.second;
      }
   }

   xmlhttp.open("GET","loadTextBox.php?id=4",true);
   xmlhttp.send();
}

loadTextBox.php code:

loadTextBox.php 代码:

<?php
---Placeholder for correct DB login info---

$result = $mysql->query("SELECT column_one FROM table_one");

while ($row = $result->fetch_object())
{
   $queryResult[] = $row->present_tense;
}
$textboxValue = $queryResult[0];
$textboxValue2 = $queryResult[2];
echo json_encode(array('first'=>$textboxValue,'second'=>$textboxValue2));
?>

回答by cube

This is fully tested and works. Use as a starting point to accomplish what you are trying to do:

这是经过全面测试和工作的。用作起点来完成您正在尝试做的事情:

var url = "YOUR.php"

var ajax = new XMLHttpRequest();
ajax.open("GET", url, true);
ajax.send(null);
ajax.onreadystatechange = function () {

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

        console.log("ready")            
        var Data = JSON.parse(ajax.responseText);
        console.log(Data);
        console.log(Data.first);

    } else {
        console.log("not ready yet")            
    }
}

This assumes your JSON output is properly formatted as you stated:

这假设您的 JSON 输出格式正确,如您所述:

{"first":"radim","second":"radi"} 

回答by programm3r

I have figured out the underlying problem. Extra tags were being sent because I had unnecessary tags in my DB info file. Those tags were being sent in the responseText with {"first":"radim","second":"radi"}. So the code pertaining to ---Placeholder for correct DB login info--- was wrong. I also changed .innerHTML to .value and now it works as intended.

我已经弄清楚了根本问题。正在发送额外的标签,因为我的数据库信息文件中有不必要的标签。这些标签在 responseText 中使用 {"first":"radim","second":"radi"} 发送。所以与 ---Placeholder for Correct DB login info 相关的代码是错误的。我还将 .innerHTML 更改为 .value,现在它按预期工作。

Main page code updated:

主页面代码更新:

function loadDoc()
{
   var xmlhttp;

   // code for IE7+, Firefox, Chrome, Opera, Safari
   if (window.XMLHttpRequest)
   {
      xmlhttp=new XMLHttpRequest();
   }
   // code for IE6, IE5
   else
   {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.onreadystatechange=function()
   {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
         var a = JSON.parse(xmlhttp.responseText);
         document.getElementById("textbox").value=a.first;
         document.getElementById("textbox2").value=a.second;
      }
   }

   xmlhttp.open("GET","loadTextBox.php?id=4",true);
   xmlhttp.send();
}