Skip to content

Instantly share code, notes, and snippets.

@ryaan-anthony
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save ryaan-anthony/01c56cd1fc794f38b561 to your computer and use it in GitHub Desktop.

Select an option

Save ryaan-anthony/01c56cd1fc794f38b561 to your computer and use it in GitHub Desktop.
Simple Ajax wrapper for Magento/Prototype.js
var AjaxBackend = Class.create();
AjaxBackend.prototype = {
initialize: function(url) {
this.url = url;
},
submit: function(){
this.request({
// build request here
});
},
request: function(params){
new Ajax.Request(this.url, {
method: 'post',
parameters: params,
onSuccess: this.finished
});
},
finished: function(response){
// handle response here
}
}
<input class="input-text" type="text" id="foo" />
<input class="input-text" type="text" id="bar" />
<button type="button" onclick="ajaxExample.submit()" class="button">
<span><span>Submit</span></span>
</button>
<script type="text/javascript">
//<![CDATA[
var ajaxExample = new AjaxBackend('<?php echo Mage::getUrl('example/controller/action') ?>');
ajaxExample.submit = function(){
this.request({
foo: $('foo').value,
bar: $('bar').value
});
};
ajaxExample.finished = function(response){
console.log(response.responseText);
};
//]]>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment