php函数的Ajax调用不发送json数组

时间:2014-04-25 01:09:45

标签: javascript php ajax json

这个javascript通过ajax用json数据类型调用php函数。从php返回的数据应该是一个包含三个项目的json数组:html,todays_events,debug_text。在linux上,返回的json不是数组。只返回最后一项debug_text,response ['html']为空。

这是ajax电话。

    $.ajax({
    url: "get_events.php",
    type: "POST",
    data: { user_id: user_id, todays_only: todays_only },
    dataType: 'json',
    cache: false,
    async: false,
    success: function (response) {
        if (response != '') 
        {
            if ( trim(response["html"]) != "" ) {
              var scroll_5_html = response["html"];
              $("#scroll_5").html(scroll_5_html);

            }
            else {
              var filter_select = document.getElementById("filter_today").checked;
              if ( filter_select == true ) {
                noevents_text += "<br/>for today";
              }
              $('#scroll_5').html('<p style="width: 140px; padding-top: 140px; padding-bottom: 131px; margin:0 auto; font-family: \'Trebuchet MS\'; font-size:12px; color:white;">'+noevents_text+'.</p>');               
            }

            todays_events = response["todays_events"];
        }
    },
    error: function (request, status, error) {
        /* alert ("status "+status+" error "+error+" responseText "+request.responseText); */
    },
});    

使用以这种方式添加的HTML创建$ print_html变量

    $print_html = '';
    $print_html .= '<div id="'.$node['id'].'" class="channel event" source="'.$node->source.'" channel_id="'.$channel_id.'" start_date="'.$start_date.'" onclick_string="'.$onclick.'"><a>'.$print_first.'</a></div>';

    $print_span = $node->title.'<br/>'.$start_date;

    if (isset($event_time) && $event_time != "" ) {
        $print_span .= ' '.$event_time.'<br/>';
    }
    $print_span .= 'Source: '.$site;
    if ( isset($node->notes) && $node->notes != "" && $node->notes != "null" ) {
        $print_span .= '<br/>'.'Notes: '.$node->notes;
    }

    $print_html .= "<script type='text/javascript'>
    var channel_top = $('.channel:last').position().top + 110;
    $('#matting').append('<span id=\"tv".$node['id']."\" class=\"tooltip\" style=\"top:'+channel_top+'px;\">".$print_span."</span>');
    </script>\n";

todays_events变量是一个数组。

$todays_events = array();

if ( $event_added_flag != 1 ) {
                                    $event_array[$nodeid.'_'.$iatt] = $todays_print_date.' '.strtolower($r_string);
                                    $start_dates[$nodeid.'_'.$iatt] = $todays_print_date;
                                    $event_times[$nodeid.'_'.$iatt] = $r_events[$iat];
                                    array_push($todays_events, $nodeid.'_'.$iatt);
 }

另一个变量$ return ['debug_text']如前所示创建。

    $return['debug_text'] .= ' r_at '.$r_at.' r_events count '.$events_count;
    $return['debug_text'] .= ' start_date '.$start_date.' print_date '.$print_date.'<br/>';

数据从php返回

$return['html'] = $print_html;
$return['todays_events'] = $todays_events;
$return['debug_text'] .= '\r\nfilter flag '.$todays_only;

echo json_encode($return);
return;

在javascript端响应[“html”]和response [“todays_events”]包含“null”。这些包含html文本。 json_encode不处理html吗?

0 个答案:

没有答案