Jquery AdobeAIR,并通过JSONP获取数据
我将javascript/jQuery与adobeair结合使用,由于某些原因,我无法让它工作,以便从服务器中提取数据 这在浏览器中运行良好(单个项目正确地附加到ul,弹出窗口显示“成功”),但当我在AdobeAIR中运行它时,它似乎不起作用(弹出窗口显示“错误”,ul没有写入) 以下是我的JS代码:Jquery AdobeAIR,并通过JSONP获取数据,jquery,ajax,air,adobe,Jquery,Ajax,Air,Adobe,我将javascript/jQuery与adobeair结合使用,由于某些原因,我无法让它工作,以便从服务器中提取数据 这在浏览器中运行良好(单个项目正确地附加到ul,弹出窗口显示“成功”),但当我在AdobeAIR中运行它时,它似乎不起作用(弹出窗口显示“错误”,ul没有写入) 以下是我的JS代码: jQuery("#whatever").append("<ul><li>test</li></ul>"); var url = 'http://ww
jQuery("#whatever").append("<ul><li>test</li></ul>");
var url = 'http://www.mysite.com/test.php?callback=?';
var data = '';
jQuery.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
alert("SUCCESS");
jQuery.each(data, function() {
jQuery.each(this, function(k, v) {
jQuery("<li></li>").html(v.siteName).appendTo("#whatever ul");
});
});
},
error: function(e) {
alert("ERROR");
console.log(e.message);
}
});
以下是如何在基于AdobeAIR的应用程序中从服务器获取数据
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="init()"
width="620" height="740"
frameRate="30">
<fx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
// var ini
// ------------------------------------------------------------------------
private var filePath:String = "http://www.mysite.com/test.php?callback=?";// URL or path to JSON Source // can be "assest/jsondata.txt";
private var urlLoader:URLLoader;
private var jsonDataArray:Array;
private var jsonObj:Object;
private var request:URLRequest;
private var variables:URLVariables;
private var requestFilters:String;
private var requestHeaders:Array;//used for content-type headers and such
private var arr:Array = new Array();// saving all objects to dataGrid
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
private function init():void
{
requestHeaders = new Array(new URLRequestHeader("content-type","application/json"));//add as many comma separated headers as you need to send to server
// Add event listener for button click
btn.addEventListener(MouseEvent.CLICK,loadJSONData);
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
private function loadJSONData(e:MouseEvent=null):void
{
// Load file
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, fileLoaded,false,0,true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, fileLoadFailed);
request = new URLRequest();
request.method = URLRequestMethod.POST;//use URLRequestMethod.GET if you want
request.requestHeaders = requestHeaders;//headers to send
request.data = 'jsonCallback';//can send data/parameters with this request to server. If server requires data/parameters sent as JSON string, you can accomplish it passing JSON String like this: request.data = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listEventTypes", "params": { "filter" : {} }, "id": 1}';
request.url = filePath;
urlLoader.load(request);
}
// ------------------------------------------------------------------------
private function fileLoadFailed(e:Event):void
{
trace("Failed: "+e.target.data.toString());
}
// ------------------------------------------------------------------------
private function fileLoaded(e:Event):void
{
// Clean up file load event listeners
urlLoader.removeEventListener(Event.COMPLETE, fileLoaded);
// If you wanted to get the data from the event use the line below
//var urlLoader:URLLoader = URLLoader(event.target);
trace("urlLoader.data: "+urlLoader.data+", Data type: "+typeof(urlLoader.data));
var Obj:Object = urlLoader.data;
// Parse the response to get Array or Object depending on how the JSON is formatted on Server.
//jsonDataArray = com.adobe.serialization.json.JSON.decode(urlLoader.data);
// converts urlLoader.data to a localized Object or Array
// check if the returned data is an Object
if(typeof(com.adobe.serialization.json.JSON.decode(urlLoader.data))=='object')
jsonObj = com.adobe.serialization.json.JSON.decode(urlLoader.data);
// if data returned is an Array
else if(typeof(com.adobe.serialization.json.JSON.decode(urlLoader.data))=='object' && com.adobe.serialization.json.JSON.decode(urlLoader.data).length>2)
jsonDataArray = com.adobe.serialization.json.JSON.decode(urlLoader.data);
/* Your Data Format
{
"sites":
[
{
"siteName": "JQUERY4U",
"domainName": "http://www.jquery4u.com",
"description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips & Code Snippets."
},
{
"siteName": "BLOGOOLA",
"domainName": "http://www.blogoola.com",
"description": "Expose your blog to millions and increase your audience."
},
{
"siteName": "PHPSCRIPTS4U",
"domainName": "http://www.phpscripts4u.com",
"description": "The Blog of Enthusiastic PHP Scripters"
}
]
}
*/
// now you should be able to access the data like this:
trace(jsonObj.sites[0].domainName);
// or
trace(jsonDataArray[0].siteName);
// Now you can loop through your received data as usual.
}
// ------------------------------------------------------------------------
]]>
</fx:Script>
<mx:DataGrid id="dg" top="100" width="600" height="600" horizontalCenter="0">
<mx:columns>
<mx:DataGridColumn width="200" dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="value" headerText="Value"/>
</mx:columns>
</mx:DataGrid>
<mx:Label top="15" color="#0D595A" fontSize="30" fontWeight="bold" horizontalCenter="4"
text="Get JSON Data From Server"/>
<mx:Button id="btn" top="70" width="200" label="Get JSON Data from Server"
chromeColor="#A7FEFF" fontWeight="bold" horizontalCenter="0"/>
</s:WindowedApplication>
2)
jsonDataArray=com.adobe.serialization.json.json.decode(urloader.data);
/*您的数据格式
{
“地点”:
[
{
“站点名称”:“JQUERY4U”,
“域名”:http://www.jquery4u.com",
“description:”#1个jQuery博客,提供每日新闻、插件、tut/Tips和代码片段。”
},
{
“站点名称”:“BLOGOOLA”,
“域名”:http://www.blogoola.com",
“说明”:“向数百万人展示你的博客,增加你的受众。”
},
{
“站点名称”:“PHPSCRIPTS4U”,
“域名”:http://www.phpscripts4u.com",
“描述”:“热情的PHP脚本编写者的博客”
}
]
}
*/
//现在,您应该能够像这样访问数据:
跟踪(jsonObj.sites[0].domainName);
//或
跟踪(jsonDataArray[0].siteName);
//现在,您可以像往常一样通过接收到的数据进行循环。
}
// ------------------------------------------------------------------------
]]>
现在,这是开始使用AdobeAIR应用程序从服务器加载JSON数据的完整代码
我正在使用导入com.adobe.serialization.json.json代码>在本例中。需要包含它,以便将以字符串格式接收的JSON数据解析/解码为本地ActionScript对象
您可以从GITHUB下载as3corelib
如果您使用Adobe Flash创建AIR应用程序,则必须在库/源路径中包含此库
如果您和我一样使用Adobe Flash Builder 4.6 Premium,那么:
1)在Adobe Flash Builder中,确保您处于Flash Builder透视图中,在Package Explorer中,右键单击项目并选择属性。(这将打开您的项目属性)
2)在项目的属性对话框中,选择“灵活构建路径”
3)在“Flex构建路径”对话框中,选择选项卡:“库路径”
4)单击右侧的“添加SWC”按钮。然后浏览到“as3corelib.swc”文件的位置。(最好将此文件放在应用程序主目录中的“libs”文件夹/目录中,“bin debug”目录也位于其中。)现在选择该文件并单击“确定”按钮
5)再次单击“确定”按钮关闭项目的“属性”对话框
6)现在保存所有项目文件和所有内容,并关闭Flash Builder
7)现在重新启动Flash Builder。运行或调试项目。您应该能够在控制台日志中看到我们的trace
语句的输出
8)如果编译项目时出现错误,则还必须包含com.adobe.serialization.json.json通过解压缩包含as3corelib的源ActionScript类的Zip文件,在项目的“src”目录中编写>包
9)现在它应该可以工作了。最好重新启动Flash Builder。现在,项目的SRC目录应该有一个com目录,其中包括as3corelib包的其他目录和ActionScript类。(至少现在我已经添加了as3corelib.swc和包的源文件,没有问题了)
在的此处下载“as3corelib”。它包含lib目录中的as3corelib.swc文件和com.adobe.serialization.json
目录中的ActionScript 3.0源类。(您可能需要将包含所有内容的com目录复制到项目根目录中的libs目录。这样您就可以在代码中调用其解码和编码方法,并将其编译到SWF和AIR文件中。)我认为您不能在AdobeAIR中使用JavaScript/jQuery。它必须是您正在开发的基于AdobeAIR的应用程序。请发布AIR应用程序的Flex/Flash Builder代码。您可以在基于AIR的应用程序中从服务器获取数据,而无需使用javascript或jQuery。我将在ActionScript3.0中为您发布示例代码,以从服务器获取数据。
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="init()"
width="620" height="740"
frameRate="30">
<fx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
// var ini
// ------------------------------------------------------------------------
private var filePath:String = "http://www.mysite.com/test.php?callback=?";// URL or path to JSON Source // can be "assest/jsondata.txt";
private var urlLoader:URLLoader;
private var jsonDataArray:Array;
private var jsonObj:Object;
private var request:URLRequest;
private var variables:URLVariables;
private var requestFilters:String;
private var requestHeaders:Array;//used for content-type headers and such
private var arr:Array = new Array();// saving all objects to dataGrid
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
private function init():void
{
requestHeaders = new Array(new URLRequestHeader("content-type","application/json"));//add as many comma separated headers as you need to send to server
// Add event listener for button click
btn.addEventListener(MouseEvent.CLICK,loadJSONData);
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
private function loadJSONData(e:MouseEvent=null):void
{
// Load file
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, fileLoaded,false,0,true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, fileLoadFailed);
request = new URLRequest();
request.method = URLRequestMethod.POST;//use URLRequestMethod.GET if you want
request.requestHeaders = requestHeaders;//headers to send
request.data = 'jsonCallback';//can send data/parameters with this request to server. If server requires data/parameters sent as JSON string, you can accomplish it passing JSON String like this: request.data = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listEventTypes", "params": { "filter" : {} }, "id": 1}';
request.url = filePath;
urlLoader.load(request);
}
// ------------------------------------------------------------------------
private function fileLoadFailed(e:Event):void
{
trace("Failed: "+e.target.data.toString());
}
// ------------------------------------------------------------------------
private function fileLoaded(e:Event):void
{
// Clean up file load event listeners
urlLoader.removeEventListener(Event.COMPLETE, fileLoaded);
// If you wanted to get the data from the event use the line below
//var urlLoader:URLLoader = URLLoader(event.target);
trace("urlLoader.data: "+urlLoader.data+", Data type: "+typeof(urlLoader.data));
var Obj:Object = urlLoader.data;
// Parse the response to get Array or Object depending on how the JSON is formatted on Server.
//jsonDataArray = com.adobe.serialization.json.JSON.decode(urlLoader.data);
// converts urlLoader.data to a localized Object or Array
// check if the returned data is an Object
if(typeof(com.adobe.serialization.json.JSON.decode(urlLoader.data))=='object')
jsonObj = com.adobe.serialization.json.JSON.decode(urlLoader.data);
// if data returned is an Array
else if(typeof(com.adobe.serialization.json.JSON.decode(urlLoader.data))=='object' && com.adobe.serialization.json.JSON.decode(urlLoader.data).length>2)
jsonDataArray = com.adobe.serialization.json.JSON.decode(urlLoader.data);
/* Your Data Format
{
"sites":
[
{
"siteName": "JQUERY4U",
"domainName": "http://www.jquery4u.com",
"description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips & Code Snippets."
},
{
"siteName": "BLOGOOLA",
"domainName": "http://www.blogoola.com",
"description": "Expose your blog to millions and increase your audience."
},
{
"siteName": "PHPSCRIPTS4U",
"domainName": "http://www.phpscripts4u.com",
"description": "The Blog of Enthusiastic PHP Scripters"
}
]
}
*/
// now you should be able to access the data like this:
trace(jsonObj.sites[0].domainName);
// or
trace(jsonDataArray[0].siteName);
// Now you can loop through your received data as usual.
}
// ------------------------------------------------------------------------
]]>
</fx:Script>
<mx:DataGrid id="dg" top="100" width="600" height="600" horizontalCenter="0">
<mx:columns>
<mx:DataGridColumn width="200" dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="value" headerText="Value"/>
</mx:columns>
</mx:DataGrid>
<mx:Label top="15" color="#0D595A" fontSize="30" fontWeight="bold" horizontalCenter="4"
text="Get JSON Data From Server"/>
<mx:Button id="btn" top="70" width="200" label="Get JSON Data from Server"
chromeColor="#A7FEFF" fontWeight="bold" horizontalCenter="0"/>
</s:WindowedApplication>