我使用JSON来解析数据并连接到PHP文件。我不确定问题是什么,因为我是flex的新手。这是我收到的错误:
JSONParseError: Unexpected < encountered
at com.adobe.serialization.json::JSONTokenizer/parseError()
at com.adobe.serialization.json::JSONTokenizer/getNextToken()
at com.adobe.serialization.json::JSONDecoder/nextToken()
at com.adobe.serialization.json::JSONDecoder()
at com.adobe.serialization.json::JSON$/decode()
at DressBuilder2/getPHPData()
at DressBuilder2/__getData_result()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()
at mx.rpc::Responder/result()
at mx.rpc::AsyncRequest/acknowledge()
at DirectHTTPMessageResponder/completeHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
以下是实际的mxml代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="535" height="345">
<mx:Script>
<![CDATA[
import mx.events.DataGridEvent;
import mx.controls.TextInput;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import com.adobe.serialization.json.JSON;
[Bindable]
private var dataArray:ArrayCollection;
private function initDataGrid():void
{
dataArray = new ArrayCollection();
getData.send();
}
private function getPHPData(event:ResultEvent):void
{
var rawArray:Array;
var rawData:String = String(event.result);
rawArray = JSON.decode(rawData) as Array;
dataArray = new ArrayCollection(rawArray);
}
private function sendPHPData():void
{
var objSend:Object = new Object();
var dataString:String = JSON.encode(dataArray.toArray());
dataString = escape(dataString);
objSend.setTutorials = "true";
objSend.jsonSendData = dataString;
sendData.send(objSend);
}
private function updatedPHPDataResult(event:ResultEvent):void
{
lblStatus.text = String(event.result);
}
private function checkRating(event:DataGridEvent):void
{
var txtIn:TextInput = TextInput(event.currentTarget.itemEditorInstance);
var curValue:Number = Number(txtIn.text);
if(isNaN(curValue) || curValue < 0 || curValue > 10)
{
event.preventDefault();
lblStatus.text = "Please enter a number rating between 0 and 10";
}
}
]]>
</mx:Script>
<mx:HTTPService id="getData" url="http://www.keishalexie.com/imd465/forum.php"
useProxy="false" method="GET" resultFormat="text"
result="getPHPData(event)">
<mx:request xmlns="">
<getTutorials>"true"</getTutorials>
</mx:request>
</mx:HTTPService>
<mx:HTTPService id="sendData" url="http://www.keishalexie.com/imd465/forum.php"
useProxy="false" method="GET" resultFormat="text"
result="updatedPHPDataResult(event)">
</mx:HTTPService>
<mx:Binding source="dgData.dataProvider as ArrayCollection"
destination="dataArray"/>
<mx:Panel x="0" y="0" width="535" height="345" layout="absolute"
title="Forum">
<mx:DataGrid id="dgData" x="10" y="10" width="495" height="241"
dataProvider="{dataArray}" creationComplete="{initDataGrid()}"
editable="true" itemEditEnd="{checkRating(event)}">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name" editable="false"/>
<mx:DataGridColumn headerText="Author" dataField="author" width="115"
editable="false"/>
<mx:DataGridColumn headerText="Rating" dataField="rating" width="50"
editable="true" />
</mx:columns>
</mx:DataGrid>
<mx:Button x="10" y="259" label="UpdateDatabase" id="butUpdate"
click="{sendPHPData()}"/>
<mx:Label x="140" y="261" id="lblStatus"/>
</mx:Panel>
</mx:Application>
这是PHP:
<?php
$USERNAME = ''; //database username
$PASSWORD = ''; //database password
$DATABASE = ''; //database name
$URL = ''; //database location
if(isset($_GET['getTutorials'])) {
mysql_connect($URL, $USERNAME, $PASSWORD);
mysql_select_db($DATABASE) or die('Cannot connect to database.');
$returnArray = array();
$query = 'SELECT * FROM Tutorials';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
array_push($returnArray, $row);
}
mysql_close();
echo json_encode($returnArray);
}
elseif(isset($_GET['setTutorials'])) {
$jsonString = urldecode($_GET['jsonSendData']);
$jsonString = str_replace("\\", "", $jsonString);
$data = json_decode($jsonString, true);
mysql_connect($URL, $USERNAME, $PASSWORD);
mysql_select_db($DATABASE) or die('Cannot connect to database.');
foreach ($data as $tutorialEntry) {
$query = sprintf(
'UPDATE Tutorials SET rating = "%s" WHERE id = "%s"',
mysql_real_escape_string($tutorialEntry['rating']),
mysql_real_escape_string($tutorialEntry['id']));
$result = mysql_query($query);
if(!$result) {
mysql_close();
echo mysql_error();
return;
}
}
mysql_close();
echo "database updated";
}
?>
答案 0 :(得分:2)
访问http://www.keishalexie.com/imd465/forum.php?getTutorials=1(您的代码正在调用的内容)返回
致命错误:调用未定义的函数:第23行/homepages/38/d177816689/htdocs/keishalexie/imd465/forum.php中的json_encode()
这不是json文档。
解决问题时,请在浏览器中访问http://www.keishalexie.com/imd465/forum.php?getTutorials=1。然后,一旦它看起来很好,就开始研究FLEX方面。
答案 1 :(得分:1)
看起来你有意想不到的&lt;在您的JSON数据中,听起来就像是从服务器获取html而不是干净的JSON。
使用firebug或http标头工具检查服务器的响应(或者只是在浏览器中加载网址),看看响应是什么。
您可能需要在输出JSON数据时禁用错误日志记录,或者如果您使用的是模板系统,则在发送JSON时不要使用它。
答案 2 :(得分:0)
看起来您的网络服务器正在抛出HTML错误页面。所以,是的..在解析之前记录您尝试解析或放置断点的JSON,并查看您尝试解析的内容。