views:

1810

answers:

2

Hi friends,

I am having a prob for ajax request in joomla using mootools.

 var url = '<?php echo JURI::base();?>index.php?option=com_test&task=getselectmode&selectedid='+$('parent_question').value;

   var params ={method: 'post',update:'test'};
  var myAjax = new Ajax(url, params);
 myAjax.request();

My prob is that, is there any to set onComplete event of the ajax request. i have set it as below on above code but nothing happen.

onComplete: function(response) { alert('Response: ' + response); }

Can you please provide full code of how to use ajax using mootools 1.1 ??

Thanks in advance

A: 

This should work:

var ajaxObj = new Ajax ('index.php?option=com_yourcomponent&view=yourview&format=raw', {
    method: "get"
});

ajaxObj.addEvent('onComplete', function (data) {
    // data is the response text
    // use as desired
});

// this initiates the call
ajaxObj.request();
jlleblanc
+2  A: 

just add the onComplete to the params object, no need to add the event seaprately. also, you can use this.response.text. it can all look a bit more compacted - depends on your preference. if you don't plan on reusing the object, just call it direct and don't assign it to a variable either:

new Ajax(url, {
    method: "get",
    update: $("someelement"),
    onComplete: function() {
       alert(this.response.text);
    }
}).request();

if you do something with the response text, you may want to remove the update: bit. if you need to evaluate the response (as javascript), use evalResponse: true instead of eval(this.response.text);. also handy - evalScripts: true|false if you want to do something from the server side along with the response.

Dimitar Christoff