Solved

NavigatetoURL and URL Request both Together doesn't work in Flex As3

Posted on 2010-08-14
12
1,729 Views
Last Modified: 2013-11-11
Hi experts,

I have created sample code here.

I need to send two calls on different URL by clicking on one Button.

In my code I have created one URL Request to some PHP link..(You can use HTTP service too..It does same problem)..When Request to URL sent , I just place another code to navigatetoURL to another link.You can see the result only works with naviagetoURL ..Call to PHP link used by URLRequest FAIL..

How can I come out from this Problem?

When I try to use two HTTPservice that calls to different URL on SingleButton Click, It fails  with first call too..How can Flex work with Concurrent Calls /multiple calls with Single Button Click..?
I need to send two calls on different URL by clicking on one Button..How to achieve this?



 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.http.HTTPService;
			
			private function Click() : void
			{
				
			var req :URLRequest = new URLRequest("http://www.snowflax.com/Bluetoad/test_clone.php");
			//FAIL CALL
     		var loader:URLLoader = new URLLoader();
     		
		     var params :URLVariables = new URLVariables();
		     params.p1 = "WHATEVER YOU WANT IT TO BE";
		     req.data = params;
		     req.method = URLRequestMethod.POST;
		     loader.load(req);
		    //PHP Call Hangs up here
		    
		     var urlRequest:URLRequest = new URLRequest("http://google.com");
			navigateToURL(urlRequest,"_blank"); 
			//Success CALL
			} 
		]]>
	</mx:Script>
	<mx:Button click="Click()">
		
	</mx:Button>
</mx:Application>

Open in new window

FAIL Call - First Call
0
Comment
Question by:mausamviki4all
  • 5
  • 5
12 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 33436810
i would recommned putting some listeners to check for errors to see where its failling

0
 
LVL 1

Author Comment

by:mausamviki4all
ID: 33440123
It doesn't FAIL but call never proceed in browser.
http://filedb.experts-exchange.com/incoming/2010/08_w33/337015/CAll-to-Service.png

Please check sample code here with browser on any sample PHP file.you will never get POST variable p1 in PHP/.NET at serverside..
0
 
LVL 1

Author Comment

by:mausamviki4all
ID: 33440124
I tried with HTTPSERVEVICE too,
 var _serv :HTTPService = new HTTPService();
            _serv.url="http://www.snowflax.com/Bluetoad/test_clone.php";
             _serv.request.data = "WHATEVER YOU WANT IT TO BE";
             _serv.method = "POST";
             _serv.send();

than
var urlRequest:URLRequest = new URLRequest("http://google.com");
            navigateToURL(urlRequest,"_blank");

POST to PHP files FAIL
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 33444251
if u don't do the navigateToURL afterwards, your POST is successful?
i don't have access to flex environment to setup test case.
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 33444270
ah wait i found an old version of flex on my desktop.
so if i click the button currently i get to google. are you saying in your case in hangs before then?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:mausamviki4all
ID: 33444494
"if u don't do the navigateToURL afterwards, your POST is successful? "
Yes If I remove naviagetoURL,I can receive POSTED data from Flex to PHP


"so if i click the button currently i get to google. are you saying in your case in hangs before then?"
I can see only Google, but if you track the call
var req :URLRequest = new URLRequest("http://www.snowflax.com/Bluetoad/test_clone.php");
was in loop..as per FireBug sceenshot :CAll-to-Service.png
 


You can experience to same thin in below Example too..You never get google.com on Button Click..that's the problem here.Call me multiple URLs never process ,only last call happens.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
       <mx:Script>
       <![CDATA[
                   import mx.controls.Alert;
            import mx.controls.Button;

            public function addButton():void {
                var myButton:Button = new Button();
                myButton.label = "New Button";
                myHBox.addChild(myButton);
               
                myButton.addEventListener(MouseEvent.CLICK,clickHandler1)
               
            }
            function clickHandler1(event:MouseEvent):void
            {
                  //Alert.show("Hai");
                  navigateToURL(new URLRequest("http://www.google.com"));
                  navigateToURL(new URLRequest("http://www.yahoo.com"));
                  
                  
            }
        ]]>
    </mx:Script>

    <mx:HBox id="myHBox" initialize="addButton();"/>

</mx:Application>
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 33444614
very interesting.
i did some searching and found the issue.
it's the asynchronous nature of AS that is the problem.
here's the fix code sniippet attached.

src of snippet.

http://www.adobe.com/livedocs/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=passingarguments_086_12.html
<?xml version="1.0"?>
<!-- wrapper/NavigateToMultipleURLS.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="openWindows(0)">
  <mx:Script><![CDATA[
     import flash.net.navigateToURL;
     private var listingData:Array=[
        "http://www.google.com", 
        "http://www.adobe.com", 
        "http://www.usatoday.com"
     ];

     private function openWindows(n: Number):void {
        if (n < listingData.length) {
           navigateToURL(new URLRequest(listingData[n]), '_blank');
           callLater(callLater, [openWindows, [n+1]]);
        }
     }
  ]]></mx:Script>   
</mx:Application>

Open in new window

0
 
LVL 1

Author Comment

by:mausamviki4all
ID: 33444716
Thanks blue-genie..

thank you for your time and answer here..I really appreciate that but i still not get rid of real problem ..Currently it works with multiple navigatetoURL but how about HTTPService and navigatetoURL here

Can you please run this example in Flex and try it..How do I works with below code..I need one/multi httpservice call to PHP file and than redirect to google.com..currently I can't get POST data into PHP files due to asynchronous nature..!!
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.http.HTTPService;
			
			private function Click() : void
			{
				
			 var _serv :HTTPService = new HTTPService();
            _serv.url="http://www.snowflax.com/Bluetoad/test_clone.php";
             _serv.request.data = "WHATEVER YOU WANT IT TO BE";
             _serv.method = "POST";
             _serv.send(); 
		    //PHP Call Hangs up here
		    
		     var urlRequest:URLRequest = new URLRequest("http://google.com");
			navigateToURL(urlRequest,"_blank"); 
			//Success CALL
			} 
		]]>
	</mx:Script>
	<mx:Button click="Click()">
		
	</mx:Button>
</mx:Application>

Open in new window

0
 
LVL 39

Expert Comment

by:blue-genie
ID: 33444753
you'll need to tweak this method

   private function openWindows(n: Number):void {
        if (n < listingData.length) {
           navigateToURL(new URLRequest(listingData[n]), '_blank');
           callLater(callLater, [openWindows, [n+1]]);
        }
     }

at work right now. will try at home for you later okay.
0
 
LVL 1

Accepted Solution

by:
mausamviki4all earned 0 total points
ID: 33445147
Finally It works with URLLoader and URLVariables.

Need to add listener wehn URL Loads,
loader.addEventListener(Event.COMPLETE,sendURL);

Send URL:
function  sendURL(event:Event):void
                  {
                  var urlRequest:URLRequest = new URLRequest("http://google.com");
                  navigateToURL(urlRequest,"_blank");
                  }


HTTPservice FAIL to load when we have parameters to send and than navigation..It does work without parameters.
If We use GET method in HTTPserive is works but FAIL when we have POST...amazing behavior.

Anyway,Check the corrected Code which works fine here..:)

Thanks blue-genie..
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.rpc.http.HTTPService;
			
			private function Click() : void
			{
				
			var req :URLRequest = new URLRequest("http://www.snowflax.com/Bluetoad/test_clone.php");
			
     		var loader:URLLoader = new URLLoader();
     		
		     var params :URLVariables = new URLVariables();
		     
		     params.e = "WHATEVER YOU WANT IT TO BE"; 
		     req.data = params;
		     req.method = URLRequestMethod.POST;
		     loader.load(req);
		    
		    loader.addEventListener(Event.COMPLETE,sendURL);
		    
			} 
			function  sendURL(event:Event):void
			{
			var urlRequest:URLRequest = new URLRequest("http://google.com");
			navigateToURL(urlRequest,"_blank");
			}
		]]>
	</mx:Script>
	<mx:Button click="Click()">
		
	</mx:Button>
</mx:Application>

Open in new window

0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 34049969
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now