使用json_encode通过xmlhttp.responseText发送特殊字符(即caron)
Sending special characters (i.e. caron) through xmlhttp.responseText using json_encode
我正试图从数据库中获取带有特殊字符(caron)的数据,并使用json_encode填充文本框,通过xmlhttp.responseText发送。与包含特殊字符(caron)的数据相关联的特定文本框不显示任何内容。其他文本框显示正确的数据。我尝试使用Javascript函数encodeURIComponent,但在文本框中只显示了null。如有任何帮助,我们将不胜感激。
主页代码:
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代码:
<?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));
?>
在发送它们之前使用UTF-8对它们进行编码。
utf8_encode($variable);
或者尝试CCD_ 2对阵列进行编码;
$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);
您可能还需要将xmlhttp.request
设置为utf-8
以及
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xmlhttp.open("GET","loadTextBox.php?id=4",true);
xmlhttp.send();
你可能需要尝试其中一种或另一种,甚至两者都尝试。你还想确认你的数据库能够存储特殊字符,如果数据库没有存储特殊字符那么你的应用程序就无法让它们编码。
在行$mysql->query中添加("SET CHARACTER SET'utf8'");在loadTextBox.php中,在连接到DB之后,在SQL查询修复它之前。
相关文章:
- javascript替换换行符和特殊字符
- angularjs删除动态形式元素中的特殊字符
- 使用json_encode通过xmlhttp.responseText发送特殊字符(即caron)
- 在搜索中考虑特殊字符的
- 避免键入一些特殊字符
- Regex测试特殊字符
- 在 Javascript 中对特殊字符进行排序 (æ)
- 必须检查长度,并在文本框中允许一些特殊字符
- 一个jquery验证器方法,它不接受纯数字或纯特殊字符,但接受上面是否有字母
- 用于在Javascript中删除多个空格的特殊字符
- jQuery-Regex以防止任何特殊字符
- javascript正则表达式来识别所有特殊字符
- RegEx只获取特殊字符,前后不获取任何字符
- 如何使用JQuery检查输入(电子邮件)字段是否包含特殊字符
- Javascript:包括特殊字符的文本选择
- 获取特殊字符后没有单词的句子
- 替换字符串javascript中的所有特殊字符..
- 在cshtml中对特殊字符进行编码
- 从JavaScript中正确返回html中的特殊字符
- AngularJS输入接受整数和特殊字符