2016-06-11 188 views
-1

我正在使用AS3在Flash CS6中構建AIR應用程序。我需要將變量Id的值發送給PHP,以便我可以選擇顯示哪些數據以及數據的顯示位置。AS3到PHP - 獲取「錯誤#1132:無效的JSON解析輸入」

我發現如何獲取已顯示的數據,哪些不是通過將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); 
} 
?> 
+0

儘量看着像提琴手工具的要求確切地看你的PHP代碼發送給你的swf。顯然,AS3 json解析器正在將您的PHP代碼的輸出視爲無效,但我們需要查看實際的json來確定原因。 – Brian

+0

好的,但我認爲錯誤在於發送變量minId的值時。因爲我試圖在php中設置minId值並正常運行。 –

+0

哪一行實際觸發錯誤? – Brian

回答

0

我不知道這是否會解決您的JSON錯誤,但我注意到您定義了phpVars(maxId & minId)AFTER你已經使用POST發送了它們。來不及更新(如:phpVars.maxId = maxId;)發送後...

嘗試建立你的代碼像下面&看看是否有幫助?

var phpVars:URLVariables = new URLVariables(); 
phpVars.maxId = maxId; 
phpVars.minId = minId; 

var phpFileReq:URLRequest = new URLRequest("http://localhost/social_media_1/timeline.php"); 
phpFileReq.data = phpVars; //# assign some data to this request 
phpFileReq.method = URLRequestMethod.POST; //# now post (send) the data 
+0

該請求不會被髮送到服務器,直到「phpLoader.load(phpFileReq);'這一行。 – Brian