Thursday, August 20, 2015

Phonegap/Cordova - Cross-domain AJAX POST Request - working example.



Cross-domain AJAX post request in 9 steps.


Step 1 – Create project
Create a phonegapproject
D:\phonegap create my_app

Step 2 – Change directory (Remember old DOS lessons?)
Change directory to your new phonegapproject
D:\cd my_app

Step 3 – Add platform
Run
D:\my_app phonegap platform add android

Step 4 – Add button




Add a button for AJAXcommunication
Open www
Edit index.html
Add a button to call web service
  
         
 <button onclick="call_ajax();">Call Web Service</button>  

Step 5 - jQuery


Download ‘jquery-1.11.3.min.js’ from http://code.jquery.com/jquery-1.11.3.min.js
Put in it ‘my_app\www\js’
Place this line just above ‘</head>’ tag in index.html
  
         
 <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>  
Step 6 - AJAX

Add this JavaScript code just above ‘</head>’ tag in index.html
  
         
 <script>  
                functioncall_ajax()  
                {  
                     $.ajax({  
                          url: 'http://flash-workshop.com/dev/php/ajax_sample.php',  
                          type: 'post',       
                          data: {  
                               'param1': phonegap,  
                               'param2': 'ajax'  
                          },  
                          headers: {  
                               Header1: '',  
                               Header2: ''  
                          },  
                          dataType: 'json',  
                          success: function (data) {  
                               alert(JSON.stringify(data));  
                          },error: function(xhr, ajaxOptions, thrownError){  
                               alert("errorstatus: " + xhr.status + " ajaxoptions: " + ajaxOptions + " throwError: " + thrownError);  
                          }  
                     });       
                }  
 </script>  
Step 7 –Run on emulator
Run
D:\my_app cordova emulate android
Click on button ‘Call Web Service’ after you see ‘device is ready’. Wait for some time.
Now you can see an error! Don’t worry it is an expected error.



Step 8 – whitelist and plugin
Open ‘config.xml’
Add the line<access origin="*" />just above ‘</widget>’ tag if it is not already there.

Add a plugin for cross-domain communication
D:\my_app phonegap plugin add cordova-plugin-whitelist


Step 9 – Conclusion
Run
D:\my_app cordova emulate android
Click on ‘Call Web Service’ button and wait for some time.

Hurray.
You did it!!
Congratulations.


Still not working?
Download and check my source file.
Happy coding.

Sunday, February 13, 2011

Flash file uploader with progress bar







ActionScript Code:





/********************************

Developed by Anil kumar N D

anilkumarnd@gmail.com

Flash Professional CS5

ActionScript 3.0

14 February 2011

*********************************/




package {

import flash.display.MovieClip;

import flash.net.FileReference;

import flash.events.*;

import flash.net.URLRequest;

import flash.net.URLVariables;




public class upload extends MovieClip {




private var _file:FileReference;





/*******************************************************************************

Default constructor

********************************************************************************/

public function upload() {

this.addEventListener(Event.ENTER_FRAME,fn_enterFrameHandler);

}




/*******************************************************************************

This function is a ENTER_FRAME event handler of "this"

input

IN_Event : Event

********************************************************************************/

private function fn_enterFrameHandler(IN_Event:Event):void{

if(this.loaderInfo.bytesTotal == this.loaderInfo.bytesLoaded){

this.removeEventListener(Event.ENTER_FRAME,fn_enterFrameHandler);

_file = new FileReference;

_file.addEventListener(Event.CANCEL, fn_cancelHandler);

_file.addEventListener(Event.COMPLETE, fn_completeHandler);

_file.addEventListener(HTTPStatusEvent.HTTP_STATUS, fn_httpStatusHandler);

_file.addEventListener(IOErrorEvent.IO_ERROR, fn_ioErrorHandler);

_file.addEventListener(Event.OPEN, fn_openHandler);

_file.addEventListener(ProgressEvent.PROGRESS, fn_progressHandler);

_file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fn_securityErrorHandler);

_file.addEventListener(Event.SELECT, fn_selectHandler);

_file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fn_uploadCompleteDataHandler);

btn_browse.addEventListener(MouseEvent.CLICK,fn_browserClickHandler);

btn_upload.addEventListener(MouseEvent.CLICK,fn_uploadClickHandler);

btn_cancel.addEventListener(MouseEvent.CLICK,fn_cancelClickHandler);

mc_progressBar.mc_bar.scaleX = 0;


}


}




/*******************************************************************************

This function is a click event handler of btn_browse

input

IN_Event : MouseEvent

********************************************************************************/

private function fn_cancelClickHandler(IN_Event:MouseEvent):void{

_file.cancel();

mc_progressBar.mc_bar.scaleX = 0;

btn_upload.enabled = false;

btn_browse.enabled = true;

btn_cancel.enabled = false;

txt_server.text = "File upload cancelled";

}




/*******************************************************************************

This function is a click event handler of btn_browse

input

IN_Event : MouseEvent

********************************************************************************/

private function fn_uploadClickHandler(IN_Event:MouseEvent):void{

btn_upload.enabled = false;

btn_browse.enabled = false;

btn_cancel.enabled = true;

_file.upload(new URLRequest("fileUpload.php"));


}




/*******************************************************************************

This function is a click event handler of btn_browse

input

IN_Event : MouseEvent

********************************************************************************/

private function fn_browserClickHandler(IN_Event:MouseEvent):void{

_file.browse();

txt_server.text = "Select your file";


}




/*******************************************************************************

These functions are the event handlers of _file

********************************************************************************/

private function fn_cancelHandler(IN_Event:Event):void {

trace("cancelHandler: " + IN_Event);

}

private function fn_completeHandler(IN_Event:Event):void {

trace("completeHandler: " + IN_Event);

btn_cancel.enabled = false;

btn_browse.enabled = true;

mc_progressBar.mc_bar.scaleX = 0;

}

private function fn_uploadCompleteDataHandler(IN_Event:DataEvent):void {

trace("uploadCompleteData: " + IN_Event);

var variables:URLVariables = new URLVariables(IN_Event.data);

txt_server.text = variables.dataFromServer;

}

private function fn_httpStatusHandler(IN_Event:HTTPStatusEvent):void {

trace("httpStatusHandler: " + IN_Event);

}

private function fn_ioErrorHandler(IN_Event:IOErrorEvent):void {

trace("ioErrorHandler: " + IN_Event);

}

private function fn_openHandler(IN_Event:Event):void {

trace("openHandler: " + IN_Event);

}

private function fn_progressHandler(IN_Event:ProgressEvent):void {

trace("progressHandler name=" + _file.name + " bytesLoaded=" + IN_Event.bytesLoaded + " bytesTotal=" + IN_Event.bytesTotal);

mc_progressBar.mc_bar.scaleX = IN_Event.bytesLoaded/IN_Event.bytesTotal;

txt_server.text = "Uploading "+ int((IN_Event.bytesLoaded/IN_Event.bytesTotal)*100) + "%";
}

private function fn_securityErrorHandler(IN_Event:SecurityErrorEvent):void {

trace("securityErrorHandler: " + IN_Event);

}

private function fn_selectHandler(IN_Event:Event):void {

txt_filename.text = _file.name;

btn_upload.enabled = true;

txt_server.text = "You have selected '"+_file.name+"'";


}



}

}




Wednesday, July 7, 2010

Sending Email from Flash using PHP















ActionScript Code:





/**************************************

Developed by

Anil kumar N D

anilkumarnd@gmail.com

http://flash-workshop.blogspot.com/

http://flash-workshop.com/


Flash CS4 professional

ActionScript 3.0

07 July,2010

**************************************/




package {

import flash.display.MovieClip;

import flash.net.URLLoader;

import flash.net.URLRequest;

import flash.net.URLRequestMethod;

import flash.net.URLVariables;

import flash.events.Event;

import flash.events.MouseEvent;




public class main extends MovieClip{

private var ldr_receive:URLLoader;

private var ldr_send:URLLoader;

private var url_variables:URLVariables;

private var urlRequest:URLRequest;




/***********************************************************************************************

Default constructor

************************************************************************************************/

public function main():void {


mc_send.mouseChildren = false;

mc_send.buttonMode = true;

mc_send.addEventListener(MouseEvent.CLICK,fn_mc_sendClickHandler);


mc_cancel.mouseChildren = false;

mc_cancel.buttonMode = true;

mc_cancel.addEventListener(MouseEvent.CLICK,fn_mc_sendClickHandler);


txt_name.tabIndex = 1;

txt_subject.tabIndex = 2;

txt_emailS.tabIndex = 3;

txt_emailR.tabIndex = 4;

txt_msg.tabIndex = 5;



}




/***********************************************************************************************



************************************************************************************************/

private function fn_mc_sendClickHandler(IN_Event:Event):void {

switch(true){

case(IN_Event.target.name == "mc_send" && (txt_name.text != "" && txt_emailS.text != "" && txt_emailR.text != "" && txt_subject.text != ""&& txt_msg.text != "")):

urlRequest = new URLRequest("flashMail.php");

url_variables = new URLVariables();

url_variables.name = txt_name.text;

url_variables.subject = txt_subject.text;

url_variables.emailSender = txt_emailS.text;

url_variables.emailReceiver = txt_emailR.text;

url_variables.message = txt_msg.text;

urlRequest.method=URLRequestMethod.POST;

urlRequest.data=url_variables;

ldr_send = new URLLoader();

ldr_send.addEventListener(Event.COMPLETE, fn_serverResponse);

ldr_send.load(urlRequest);

break;

case(IN_Event.target.name == "mc_cancel"):

txt_status.text= "";

txt_name.text = "";

txt_emailS.text = "";

txt_emailR.text = "";

txt_msg.text = "";

txt_subject.text = "";

break;


}

}




/***********************************************************************************************



************************************************************************************************/

private function fn_serverResponse(IN_Event:Event):void {

txt_name.text = "";

txt_emailS.text = "";

txt_emailR.text = "";

txt_msg.text = "";

txt_subject.text = "";

ldr_receive = URLLoader(IN_Event.target);

url_variables = new URLVariables(ldr_receive.data);

txt_status.text= url_variables.returnValue;


}




}

}