AS3到PHP - 获取“错误#1132:无效的JSON解析输入”

时间:2016-06-11 13:34:48

标签: php mysql json actionscript-3

我正在使用AS3在Flash CS6中构建AIR应用程序。我需要将变量Id的值发送给PHP,以便我可以选择显示哪些数据以及数据的显示位置。

我已经找到了如何获取已显示的数据,而不是通过将Id值输入数组并获取最高值或最低值来获取数据。

但问题是,当我将数据发送到php时,我收到错误:SyntaxError: Error #1132: Invalid JSON parse input

这是我的代码:

var maxId:Number = 0;
var minId:Number = 18;
var phpVars:URLVariables = new URLVariables();
var phpFileReq:URLRequest = new URLRequest("http://localhost/social_media_1/timeline.php");
phpFileReq.method = URLRequestMethod.POST;
phpFileReq.data = phpVars;
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpVars.maxId = maxId;
phpVars.minId = minId;
phpLoader.load(phpFileReq);

bg2_mc.x = 0;
bg2_mc.y = 305;
addChild(bg2_mc);
timeline_mc.x = 0;
timeline_mc.y = 305;
addChild(timeline_mc);
sampul_mc.x = 0;
sampul_mc.y = 0;
addChild(sampul_mc);
fotoProfil_mc.x = 240;
fotoProfil_mc.y = 85;
addChild(fotoProfil_mc);
navigasi_mc.x = 0;
navigasi_mc.y = 170;
addChild(navigasi_mc);
tempatPost_mc.x = 0;
tempatPost_mc.y = 230;
addChild(tempatPost_mc);
tempatPost_mc.btn.gotoAndStop(2);
tombol1.x = tombol1.width/2;
tombol1.y = tombol1.height/2;
addChild(tombol1);
tombol2.x = tombol2.width/2;
tombol2.y = 100;
addChild(tombol2);

loadTimeline()

function loadTimeline(){
    var phpFileRequest:URLRequest = new URLRequest("http://localhost/social_media_1/timeline.php?action=load_timeline");
    var phpLoader:URLLoader = new URLLoader();
    phpLoader.dataFormat = URLLoaderDataFormat.TEXT;
    phpLoader.addEventListener(Event.COMPLETE, onCompleteLoadTimeline);
    phpLoader.load(phpFileRequest);
}

function onCompleteLoadTimeline(event:Event){
    var result:Object = JSON.parse(event.target.data);

    for (var i:int=0;i<=4;i++){
        tpostArr[i] = new t_post();
        var batas_mc:batas = new batas();

        tpostArr[i].foto.x = 52.50;
        tpostArr[i].foto.y = 52.50;

        tpostArr[i].nama.x = 120;
        tpostArr[i].nama.y = 20;

        tpostArr[i].postingan.x = 120;
        tpostArr[i].postingan.y = 55.15;
        tpostArr[i].postingan.wordWrap = true;
        tpostArr[i].postingan.autoSize = TextFieldAutoSize.LEFT;

        tpostArr[i].tombol_suka.x = 440;
        tpostArr[i].tombol_suka.y = 73;
        tpostArr[i].nama.text = result[i].timeline_name;
        tpostArr[i].postingan.text = result[i].timeline_post;
        batas_mc.x = 0;
        batas_mc.y = tpostArr[i].postingan.y + tpostArr[i].postingan.height + 20;
        tpostArr[i].addChild(batas_mc);
        idPrint.push(result[i].id);

        timeline_mc.addChild(tpostArr[i]);
        tpostArr[i].y = yPos;
        yPos = tpostArr[i].y+tpostArr[i].height;
        yPos +=5;
        maxId = Math.max.apply(null, idPrint);
        minId = Math.min.apply(null, idPrint);

        var phpVars:URLVariables = new URLVariables();
        var phpFileReq:URLRequest = new URLRequest("http://localhost/social_media_1/timeline.php");
        phpFileReq.method = URLRequestMethod.POST;
        phpFileReq.data = phpVars;

        phpVars.maxId = maxId;
        phpVars.minId = minId;
    }
}

这是我的PHP代码:

<?php
require_once "connect.php";
$action = isset($_GET['action'])?$_GET['action']:'';
$body_nama = array();
$body_postingan = array();
$total_likers = array();
$id = array();
$minId = URLDecode($_POST['minId']);

switch($action){
case 'posting':
posting();
break;
case 'like':
like();
break;
case 'delet_ini':
deletIni();
break;
case 'load_timeline':
loadTimeline();
break;
case 'load_timeline_baru':
loadTimelineBaru();
break;
}


function loadTimeline(){

global $minId;

$query_offset = "SELECT COUNT(*) FROM timeline_posts WHERE id > $minId";
$result_offset = mysql_query($query_offset);
$offset = mysql_result($result_offset,0);

echo $offset;

$jumlah = 10;
$sqldata = mysql_query("SELECT * FROM timeline_posts ORDER BY timeline_posts.id DESC LIMIT $offset,$jumlah");


$rows = array();
while($r = mysql_fetch_assoc($sqldata)) {
    $rows[] = $r;
}



echo json_encode($rows);

}

function loadTimelineBaru(){
$maxId = URLDecode($_POST['maxId']);


$query_load = "SELECT COUNT(*) FROM timeline_posts WHERE id > '$maxId'";
$result_load = mysql_query($query_load);
$jumlah = mysql_result($result_load,0);
$sqldata = mysql_query("SELECT * FROM timeline_posts ORDER BY timeline_posts.id ASC LIMIT '$jumlah'");

$rows = array();
while($r = mysql_fetch_assoc($sqldata)) {
    $rows[] = $r;
}

echo json_encode($rows);
}
?>

1 个答案:

答案 0 :(得分:0)

我不知道这是否会修复您的JSON错误,但我注意到您使用{{1}定义了phpVars(maxId&amp; minId) AFTER }。发送后更新太晚(例如:POST

尝试设置您的代码,如下所示&amp;看看它是否有帮助...

phpVars.maxId = maxId;