jquery脚本正在打印"
和,
为什么?
php脚本:
<?php
session_start();
include 'classes/datahandle.class.php';
$data = new Datahandle();
$query = "SELECT i.instructionValue FROM INSTRUCTION_INPUT i WHERE i.deviceId = '$_SESSION[deviceId]' AND i.instructionState = '1' ORDER BY inputPortNumber";
$inputResult = $data->selectDataQuery($query);
while ($inRow = mysql_fetch_array($inputResult)){
$array[] = $inRow[0];
}
header("Content-type: text/plain");
echo json_encode($array);
?>
jquery脚本:
<script>
$(document).ready(function() {
var refreshId = setInterval(function() {
$.get('response.php', function(data) {
$.each(data, function(index, value) {
$('#value'+index).html(value).show();
});
});
}, 3000);
$.ajaxSetup({ cache: false});
});
</script>
HTML:
<tbody><tr style="color: white; font-weight: bold;">
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value0">[</td>
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value1">"</td>
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value2">1</td>
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value3">1</td>
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value4">"</td>
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value5">,</td>
</tr>
<tr>
<td style="text-align: center; color: white; font-weight: bold;" id="value6">"</td>
</tr>
</tbody>
答案 0 :(得分:2)
您需要使用$.getJSON
来解析从AJAX请求中检索到的JSON对象,而不是尝试处理原始字符串:
$.getJSON('response.php', function(data) {
另外,正如@Rocket建议的那样,您应该将header("Content-type: text/plain");
更改为header("Content-type: application/json");
中的response.php
。
这是没有解析JSON的版本,它会重现您的问题:http://jsfiddle.net/XQ7cX/。
以下是解析JSON的版本,它正确显示:http://jsfiddle.net/XQ7cX/1/