Topic: How to get a success / callback through xajax?

I am new to xajax as I am working for a new company and they use xajax instead of jQuery.

In jQuery, it is very easy to get the success callback 'data' of the file which you have called.

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    alert(data);
  }
});

How can I do this with this xajax syntax? This does not work:

xajax.call('order_displaySomething', {
    parameters:[someparameter],
        onComplete: (function(data){
            alert(data);
    })
});

Re: How to get a success / callback through xajax?

Hi,

Look in delayEvents.php in the tests/suites folder of the xajax 0.5 download. It demonstrates how to setup and use numerous different client side callbacks.

Here's a chunk of it:

            setupCallbacks = function() {
                xajax.callback.global.onRequest = function() {
                    clearCheckboxes();
                    xajax.$('global_onRequest').checked = 'checked';
                }
                xajax.callback.global.onResponseDelay = function() {
                    xajax.$('global_onResponseDelay').checked = 'checked';
                }
                xajax.callback.global.onExpiration = function() {
                    xajax.$('global_onExpiration').checked = 'checked';
                }
                xajax.callback.global.beforeResponseProcessing = function() {
                    xajax.$('global_beforeResponseProcessing').checked = 'checked';
                }
                xajax.callback.global.onFailure = function() {
                    xajax.$('global_onFailure').checked = 'checked';
                }
                xajax.callback.global.onRedirect = function() {
                    xajax.$('global_onRedirect').checked = 'checked';
                }
                xajax.callback.global.onSuccess = function() {
                    xajax.$('global_onSuccess').checked = 'checked';
                }
                xajax.callback.global.onComplete = function() {
                    xajax.$('global_onComplete').checked = 'checked';
                }
                
                local = {};
                local.callback = xajax.callback.create(360, 12000);
                local.callback.onRequest = function() {
                    xajax.$('local_onRequest').checked = 'checked';
                }
                local.callback.onResponseDelay = function() {
                    xajax.$('local_onResponseDelay').checked = 'checked';
                }
                local.callback.onExpiration = function(oRequest) {
                    xajax.$('log').innerHTML += 'request timed out...<br />';
                    xajax.$('local_onExpiration').checked = 'checked';
                    xajax.abortRequest(oRequest);
                    if (1 < oRequest.retry) {
                        --oRequest.retry;
                        xajax.$('log').innerHTML += 'starting retry of current request<br />';
                        clearCheckboxes();
                        xajax.request(oRequest.functionName, oRequest);
                    } else
                        xajax.$('log').innerHTML += 'retry attempts exhausted, request failed<br />';
                }
                local.callback.beforeResponseProcessing = function() {
                    xajax.$('local_beforeResponseProcessing').checked = 'checked';
                }
                local.callback.onFailure = function() {
                    xajax.$('local_onFailure').checked = 'checked';
                }
                local.callback.onRedirect = function() {
                    xajax.$('local_onRedirect').checked = 'checked';
                }
                local.callback.onSuccess = function() {
                    xajax.$('local_onSuccess').checked = 'checked';
                }
                local.callback.onComplete = function() {
                    xajax.$('local_onComplete').checked = 'checked';
                }
                
                special = {}
                special.callback = xajax.callback.create(6000, 15000);
                special.callback.onResponseDelay = function() {
                    xajax.$('special_onResponseDelay').checked = 'checked';
                }
                special.callback.onComplete = function() {
                    xajax.$('special_onComplete').checked = 'checked';
                }
            }

You setup the ones you want to use and call your xajax functions using events or the xajax.call and the callback is called for the onxxx event.

If you give me an idea of just what you need to do I can probably gin up some sample code...

Hope this helps.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

3 (edited by xMike 2012-08-31 10:06:22 AM)

Re: How to get a success / callback through xajax?

Hi Ed,

Thank you for the answer, which looks a little bit "complicated" for someone who is not an expert in programming. All I need to do is to get the return value from the order_displaySomething method... the PHP return value can be just a string or an array. No matter which type the return value is, I want to just alert() it for test reasons.

Coming back to the example I provided above, I just want to alert(data) the return value from the order_displaySomething call in an easy way.

How can I do this?

Thanks, again.

Re: How to get a success / callback through xajax?

In your xajax function you declare an xajax response object and use it to do the alert.

<?php
include 'xajax/xajax_core/xajax.inc.php';
$xajax = new xajax();
$xajax->confugure('javascript URI', 'xajax/');

myFunction($someparameter)
{
  $resp = new xajaxResponse();
  --- do something---
  $resp->alert(something)
  return $resp;
}

$xajax->register(XAJAX_FUNCTION, 'myFunction');
$xajax->processRequest()
<?
<!DOCTYPE html>
<html>
<head>
<?php $xajax->printJvascript(); ?>
</head>
<body>
<input type="button" value="Click Me" onclick="xajax_myFunction('somevalue')"/>
</body>
</html>

When the button is clicked it calls myFunction which returns an alert command.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.