dongzai3139
2012-09-11 16:16
浏览 107

使用json_encode通过xmlhttp.responseText发送特殊字符(即caron)

I'm trying to grab data with a special character (caron) from a database and send it through xmlhttp.responseText using json_encode to populate textboxes. The specific textbox associated with the data containing the special character (caron) is displaying nothing. The other textboxes are displaying correct data. I tried using the Javascript function encodeURIComponent, but only null showed in the textbox. Any help would be appreciated.

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 a = JSON.parse(xmlhttp.responseText);
         document.getElementById("textbox").value=a.first;
         document.getElementById("textbox2").value=a.second;
         document.getElementById("textbox3").value=a.third;
         document.getElementById("textbox4").value=a.fourth;
         document.getElementById("textbox5").value=a.fifth;
         document.getElementById("textbox6").value=a.sixth;
      }
   }

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

loadTextBox.php code:

<?php
header("Content-type: application/json");

---Placeholder for correct DB login info---

$result = $mysql->query(---Placeholder for correct SQL query---);

while ($row = $result->fetch_object())
{
   $queryResult[] = $row->colun_one;
}
$textboxValue = $queryResult[0];
$textboxValue2 = $queryResult[1];
$textboxValue3 = $queryResult[2];
$textboxValue4 = $queryResult[3];
$textboxValue5 = $queryResult[4];
$textboxValue6 = $queryResult[5];
echo    
json_encode(array('first'=>$textboxValue,'second'=>$textboxValue2,
'third'=>$textboxValue3,'fourth'=>$textboxValue4,'fifth'=>$textboxValue5,
'sixth'=>$textboxValue6));
?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duandie5707 2012-09-16 04:59
    已采纳

    Adding in the line $mysql->query("SET CHARACTER SET 'utf8'"); inside loadTextBox.php after connecting to the DB and before the SQL query fixed it.

    已采纳该答案
    打赏 评论
  • dongliming2416 2012-09-11 16:22

    Encode them using UTF-8 before sending them.

    utf8_encode($variable);

    or try array_map(); to encode the array;

    $textboxValue = $queryResult[0];
    $textboxValue2 = $queryResult[1];
    $textboxValue3 = $queryResult[2];
    $textboxValue4 = $queryResult[3];
    $textboxValue5 = $queryResult[4];
    $textboxValue6 = $queryResult[5];
    $arrayToEncode = array('first'=>$textboxValue,'second'=>$textboxValue2,
    'third'=>$textboxValue3,'fourth'=>$textboxValue4,'fifth'=>$textboxValue5,
    'sixth'=>$textboxValue6);
    $encodedArray = array_map('utf8_encode', $arrayToEncode);
    echo json_encode($encodedArray);
    

    you may also need to set your xmlhttp.request to utf-8 as well

    xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
    xmlhttp.open("GET","loadTextBox.php?id=4",true);
    xmlhttp.send();
    

    You may need to try one or the other or even both. You also want to confirm your DB is able to store special characters, if the DB is not storing the special chars then there is no way your application will be able to get them to encode them.

    打赏 评论

相关推荐 更多相似问题