我无法使用JSON从ajax解码数组,我做错了什么

时间:2013-08-29 03:46:41

标签: php javascript jquery ajax json

我创建数据并将其返回OK但是五个小时后我一直在尝试解码它。我已经尝试了我能找到的每个例子,但每次都会出现不同的错误。我已经尝试过数据类型“json”和“text json”也将json添加到调用中(我读到某处自动解码它)返回的数据很好但是我需要将它提取到单独的变量中。我发现了一些jscript代码在服务器上生成数组,但也失败了。

function getadvertxml(idprodxxx, offset, idwho) {
    $.ajax({
        url: 'xml_getadverts.php',
        dataType: 'json',
        data: {
            "idprodxxx": idprodxxx,
            "offset": offset,
            "idwho": idwho,
        },
        type: 'GET',
        success: function (data) {
            //          data = JSON_decode(data,true);
            data = JSON.parse(data);
            console.data;
            //                  $('#content_sidebar').html(data[5]);
        }
    });
}

服务器

  <CREATE DATA works OK checked on the console log>.......
  $arr[0]=$playerxy;
  $arr[1]="video16";//linkid
  if ($video1) {
    $arr[2]=$video1->urlvideo;//playerurl
    $arr[3]=$video1->urlwho;//linkurl
    $arr[4]=$video1->whodesc;//linkdes
  } else {
    $arr[2]="../../videos/default." . $ext;
    $arr[3]="http://notfound.com.au/";
    $arr[4]="Not found";
  }  
  $arr[5]=$HTML5;
  $arr=json_encode($arr);
  echo ("$arr");

这个特定版本的错误是"SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data"但我认为我必须已经生成了几乎所有的JSON错误,我试图让它工作。当我开始撕掉我的白发时请帮忙。

编辑

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    require_once("../../includes/initialize.php");
    require_once("../../includes/functions.php");
    require_once("../../includes/session.php");
    require_once("../../includes/database.php");
    require_once("../../includes/pageination.php");
    if ($session->is_logged_in()) {
        include '../../includes/load3who.php';
        $idworldview=$_GET['idworldview'];
        $idprodxxx=$_GET['idprodxxx'];
        $cat=$_GET['cat'];
        $perpage=$_GET['perpage'];
        $offset=$_GET['offset'];
        $page=$_GET['page'];
        $levela=$_GET['levela'];
        $idwho=$_GET['idwho'];
        $playerxy=1;
        $ext='ogv';
        $idworld=41;
        $reports = NULL;
        $find="|?|";
        $groupdesc= substr($cat,0,strrpos($cat, $find));                
        $catdesc= " " . substr($cat,strrpos($cat, $find) + 3);
        $HTML5="<center><small><h3>{$groupdesc}</h3></small></center>"; 
        $virgin = true; 
        $synopis = "";
            $pageination = new Pageination($page, $perpage);
            $reports = Report_as::find_reports($idworldview,$idprodxxx,$perpage,$offset);
            $ctreports=count($reports);
            $pageination->seteof($ctreports<=$perpage);
            if(!$pageination->iseof()){
                unset($reports[$perpage]); //remove extra row - retrieved to see if EOF
            }   
        foreach($reports as $rp) {
            if ($virgin) {
                $HTML5.='<table class="reporttable" border="1" width=100%>';
                $HTML5.= '<tr>';
                    $HTML5.= "<th align=center>{$catdesc}</th>";
                    $HTML5.= '<th align=center>Visit Website</th>';
                $HTML5.= '</tr>';
                $virgin = false;
            } else {
                $HTML5.= "</td>"; 
                $HTML5.= "</tr>";
            }
            $HTML5.= "<tr height=60px; max-height=60px; vertical-align=top>";
            $HTML5.= "<td align=top width= 70%>";
            $cust_rp = Report_as::find_cust_reports($rp->idprodcgs, $rp->idwho_cust);
            if (!$cust_rp) {
                $cust_rp = Report_as::find_cust_fallback($rp->idwho_cust);
            }
            $custurl = '<a href="' . $rp->urlcust . '"target="_blank">' . $rp->custdesc . '</a>';
            $HTML5.= '<a href="javascript:changesrc(\''.
                                        str_replace("'", "\\'",$rp->urlvideo).'\', \''.
                                        str_replace("'", "\\'",$cust_rp[0]->urlcustvideo).'\', \''.
                                        str_replace("'", "\\'",$cust_rp[0]->custdesc).'\', \''.
                                        str_replace("'", "\\'",$cust_rp[0]->urlcust) . '\')">' . $rp->synopsis . '</a>';
            $HTML5.= "</td>";
            $HTML5.= "<td>";
            $HTML5.= '<a href="' . $rp->urlwho . ' "target="_blank">' . $rp->mydesc .'</a>';
            if($levela == 'x') {
                $idprodxxx = $rp->idprodcgs;
            }
            $whoproducturl = Report_as::find_whoproducturl($rp->idwho, $idprodxxx);
            if ($whoproducturl) {
                foreach($whoproducturl as $wp) {
                    $HTML5.= '<br><a href="' . $wp->urlprod . ' "target="_blank">' . $wp->urldesc .'</a>';
                }
            }
        }
        $HTML5.="</td></tr></table><small><strong><center>";
        $HTML5.='<a href="javascript:emailme(\''.
                                        str_replace("'", "\\'", $user_id).'\', \''.
                                        str_replace("'", "\\'", $idprodxxx) . '\')">' . 'Email me' . '</a> when new reports are added to this category</center></strong></small>'; 

        $HTML5.="<input type='button' name='back' style='float:left' value=' &laquo; Menu 'onclick='location.href=\"index.php\"'/>";
        if($reports){
            $nextdisable=" disabled ";
            $prevdisable=" disabled ";
            $next1=" No more ";
            $prev1=" No more ";
                $holdoffset=$offset;
                $holdpage=$page;
                $updown='N';
                $nextdisable=" ";
                $next1=" &dArr; Next &dArr; ";
                $offset=$holdoffset+$perpage;
                $page=$holdpage+1;
                if($pageination->iseof()) {
                    $nextdisable=" disabled ";
                    $next1=" Finished ";
                }  
                $HTML5.="<input{$nextdisable}id=\"nbutton\" type=\"button\" name=\"next\" style= \"float:right\" value=\"{$next1}\" onclick=\"getreports('".
                                    str_replace("'", "\\'",$idworldview).'\', \''.
                                    str_replace("'", "\\'",$idprodxxx).'\', \''.
                                    str_replace("'", "\\'",$cat).'\', \''.
                                    str_replace("'", "\\'",$levela).'\', \''.
                                    str_replace("'", "\\'",$perpage).'\', \''.
                                    str_replace("'", "\\'",$offset).'\', \''.
                                    str_replace("'", "\\'",$page).'\', \''.
                                    str_replace("'", "\\'",$idwho).'\', \''.
                                    str_replace("'", "\\'",$playerxy).'\', \''.
                                    str_replace("'", "\\'",$ext).'\', \''.
                                    str_replace("'", "\\'",$idworld).'\'' . ")\"/>";

                if($pageination->haspreviouspage()) {
                    $updown='P';
                    $prevdisable=" ";
                    $prev1=" &uArr; Back &uArr; ";
                    $offset=$holdoffset-$perpage;
                    $page= $holdpage-1;
                    $HTML5.="<input{$prevdisable}type=\"button\" name=\"next\" style= \"float:right\" value=\"{$prev1}\" onclick=\"getreports('".
                                        str_replace("'", "\\'",$idworldview).'\', \''.
                                        str_replace("'", "\\'",$idprodxxx).'\', \''.
                                        str_replace("'", "\\'",$cat).'\', \''.
                                        str_replace("'", "\\'",$levela).'\', \''.
                                        str_replace("'", "\\'",$perpage).'\', \''.
                                        str_replace("'", "\\'",$offset).'\', \''.
                                        str_replace("'", "\\'",$page).'\', \''.
                                        str_replace("'", "\\'",$idwho).'\', \''.
                                        str_replace("'", "\\'",$playerxy).'\', \''.
                                        str_replace("'", "\\'",$ext).'\', \''.
                                        str_replace("'", "\\'",$idworld).'\'' . ")\"/>";
                }
        }
        $ix = -1;
        $kazvideo1 = p4::find_kazand(1,'ogv',41); // 1=top video
                $arr[0]=$playerxy;
                $arr[1]="video16";//linkid
                if (!$kazvideo1) {
                    $ixpos = rand(0,count($kazvideo1)-1);
                    $arr[2]=$kazvideo1[$ixpos]->urlvideo;//playerurl
                    $arr[3]=$kazvideo1[$ixpos]->urlwho;//linkurl
                    $arr[4]=$kazvideo1[$ixpos]->whodesc;//linkdes
                } else {
                    $arr[2]="../../videos/default." . $ext;
                    $arr[3]="http://notfound.com.au/";
                    $arr[4]="Not found";
                }  
                $arr[5]=$HTML5;
                $arr=json_encode($arr);
                die ("$arr");
    }
    ?>

1 个答案:

答案 0 :(得分:0)

服务器端:

$arr[0]=$playerxy;
$arr[1]="video16";//linkid
if ($video1) {
    $arr[2]=$video1->urlvideo;//playerurl
    $arr[3]=$video1->urlwho;//linkurl
    $arr[4]=$video1->whodesc;//linkdes
} else {
    $arr[2]="../../videos/default." . $ext;
    $arr[3]="http://notfound.com.au/";
    $arr[4]="Not found";
}  
$arr[5]=$HTML5;
die(json_encode($arr));

客户端:

function getadvertxml(idprodxxx,offset,idwho){
    $.ajax({
        url : 'xml_getadverts.php',
        dataType: 'json',
        data:{ "idprodxxx":idprodxxx,
            "offset":offset,
            "idwho":idwho },
        type: 'GET',
        success: function(data){
            console.log(JSON.stringify(data))
        }
    });
}