вывести значения массива php с помощью jquery? - PullRequest
0 голосов
/ 04 мая 2011

jquery-скрипт printng " и , почему?

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>

1 Ответ

2 голосов
/ 04 мая 2011

Вам нужно использовать $.getJSON, который анализирует объект JSON, полученный из запроса AJAX, вместо попытки обработки необработанной строки:

$.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/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...