:: Forum >> Version 2 >>

AW is SUPPORTING remote function calling

I use trial AW to know the feuture and I modified Sajax (from http://modernmethod.com), and i can call remote function (in php server). so i can call function remotely. i can also use URL that contain ? in setURL with method GET and POST (in default, if i use GET with URL index.php?foo=1 and parameter var=2, the URL become index.php?foo=1?var=2, if we use index.php?foo=1 as URL in POST, the foo is empty).
here's my stupid code:
<script>
var sajax_debug_mode = false;
var sajax_request_type = "GET";
var sajax_target_id = "";
var sajax_failure_redirect = "";

function sajax_debug(text) {if (sajax_debug_mode)alert(text);}
var sajax_requests = new Array();
function sajax_cancel() {for(var i = 0; i < sajax_requests.length; i++)sajax_requests[i].abort();}
function sajax_do_call(func_name, args) {
var i,n;var uri;var post_data;var target_id;var x = args[0];
sajax_debug("in sajax_do_call().." + sajax_request_type + "/" + sajax_target_id);
target_id = sajax_target_id;
if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "" || (sajax_request_type != "GET" && sajax_request_type != "POST"))
sajax_request_type = "GET";
uri = "/kpu/";
if(sajax_request_type=='POST'){
x.setURL(uri);
x.setRequestMethod(sajax_request_type);
}
if(sajax_request_type=='GET'){
if (uri.indexOf("?") == -1) {
x.setURL(uri);
x.setRequestMethod(sajax_request_type);
}else{
var ura=uri.split('?');
x.setURL(ura[0]);
x.setRequestMethod(sajax_request_type);
ur=ura[1].split('&');
for(i=0;i<ur.length;i++){
ur1=ur[i].split('=');
x.setParameter(ur1[0],escape(ur1[1]));
}
}
}
x.setParameter('rs',escape(func_name));
x.setParameter('rst',escape(sajax_target_id));
x.setParameter('rsrnd',new Date().getTime());
var dtparam=[];
for (i = 1; i < args.length; i++) {
dtparam[i-1]=escape(args[i]);
}
x.setParameter("rsargs[]", dtparam);
sajax_requests[sajax_requests.length] = x;
return true;
}

function x_return_object() {sajax_do_call("return_object",x_return_object.arguments);}

//call the function
function display_result(val) {
var repr;

repr = "";
repr += "Type: " + typeof val + "\n";
repr += "Value: ";
if (typeof val == "object" ||
typeof val == "array") {
repr += "{ ";
for (var i in val)
repr += i + ": " + val[i] + ", ";
repr = repr.substr(0, repr.length-2) + " }";
} else {
repr += val;
}
alert(repr);
}
var req = new AW.HTTP.Request;
x_return_object(req);
//alert(req.getResponseText());
req.response = function(text) {
var status;
var data;
var txt = text.replace(/^\s*|\s*$/g,"");
status = txt.charAt(0);
data = eval(txt.substring(2));//perhatikan eval
//alert(data);
display_result(data);
};
req.request();
//end call function

</script>

and in the php source:
function return_object() {
class MyObj {
var $name, $age;

function MyObj($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$o = new MyObj("Tom", 26);
return $o;
}
$sajax_request_type = "GET";
//$sajax_debug_mode = 1;
sajax_init();

sajax_export("return_object");
sajax_handle_client_request();//this function to handle the request, end ending with exit.

this is usefull for calling remote function in my global function file (in php source). and i can use AW ajax with URL index.php?foo=1?foo=2 without sparate new file to handle AW ajax.

Any comment or better code??thx
winotosw
Monday, April 23, 2007



This topic is archived.

Back to support forum

Forum search