As a known fact, data calls with Flex is asynchronous. HttpService, RemoteService or WebService components is not able to be used in Flex. In this article, we give some useful information about synchronous communication with Flex.
Basically, what we are about to do is creating XMLHttpRequest with Javascript in Flex, and calling a server data with the parameters we will give to the object.
As a known fact, data calls with Flex is asynchronous.
HttpService, RemoteService or WebService components is not able to be used in Flex.
In this article, we give some useful information about synchronous communication with Flex.
Basically, what we are about to do is creating XMLHttpRequest with Javascript in Flex,
and calling a server data with the parameters we will give to the object.
XMLHttpRequest takes 3 parametes:
1. Request Type: GET or POST
2. Requested URL
3. Communication Type: true for asynchronous, false for synchronous.
For example: xmlHttpRequest.open("GET","http://localhost/Default.aspx",false);
We can use a synchronous communication using the Ajax.as and AjaxRequestType.as classes below.
We can call the URL we give to the "send" method in Ajax.as class,
and get a synchronous response from the server.
package fleXtense.core
{
import flash.external.ExternalInterface;
public class Ajax
{
private var _url:String = "";
private var _async:Boolean=false;
private var _responseText:String;
private var _requestType:AjaxRequestType;
public function Ajax(url:String)
{
this._url = url;
}
public function get async():Boolean
{
return this._async;
}
public function set async(value:Boolean):void
{
this._async = value;
}
public function get responseText():String
{
return this._responseText;
}
public function get requestType():AjaxRequestType
{
return this._requestType;
}
public function set requestType(value:AjaxRequestType):void
{
this._requestType = value;
}
public function send(data:String=null):String
{
var sendingData:String = sendingData(data);
this._responseText = ExternalInterface.call(sendingData);
return this._responseText;
}
private function sendingData(data:String=null):String
{
if(ExternalInterface.available)
{
var data:String = "function()" +
"{" +
"var xmlHttp;"+
"try" +
"{"+
" xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');" +
"}"+
"catch(e)" +
"{" +
"try" +
"{" +
"xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');" +
"}" +
"catch(oc)" +
"{" +
"xmlHttp=null;" +
"}" +
"}" +
"if(!xmlHttp && typeof XMLHttpRequest != 'undefined')" +
"{" +
"xmlHttp=new XMLHttpRequest();" +
"}"+
"try" +
"{" +
"xmlHttp.open('"+ requestType.toString() + "','" + _url + "'," + async + ");"+
"xmlHttp.send("+ data + ");"+
"return xmlHttp.responseText;" +
"}"+
"catch(x){alert(x)}" +
"}";
return data;
}
else
{
throw new Error("This browser is not supported.");
}
}
}
}
AjaxRequestType.as
package fleXtense.core
{
public class AjaxRequestType
{
private var item:String;
public static var GET:AjaxRequestType = new AjaxRequestType("GET");
public static var POST:AjaxRequestType = new AjaxRequestType("POST");
public function AjaxRequestType(item:String=null)
{
this.item = item;
}
public function toString():String
{
return this.item;
}
}
}
For testing the classes, an Test.mxml demo is shown below.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="327" height="180">
<mx:Script>
<![CDATA[import fleXtense.core.AjaxRequestType;
import fleXtense.core.Ajax;
import mx.controls.Alert;
private function callSyncronRequest():void
{
var url:String = 'http://localhost:2935/Default.aspx?name=' + nameTextInput.text;
var ajax:Ajax = new Ajax(url);
ajax.requestType = AjaxRequestType.GET;
ajax.async = false;
var response:String = ajax.send();
Alert.show(response);
}]]>
</mx:Script>
<mx:TextInput x="10" y="19" id="nameTextInput"/>
<mx:Button x="173.5" y="19" label="Call Synchoron " click="callSyncronRequest();"/>
</mx:Application>
And lastly the Default.aspx.cs page's code is here.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.QueryString.Get("name");
Response.ContentType = "text/html";
Response.AddHeader("Cache-Control", "no-cache");
Response.Output.Write("Hello " + name);
}}
Note:
You cannot use this demo with a local file. To test it,
you should use it by creating a virtual directory or publish it on a virtual directory
on IIS.
Visit the site:http://www.flextense.net