Jump to content

Appel à une fonction Ajax dans un Controller admin à partir d'un tpl


Recommended Posts

Bonjour,  voici mon problème : 

 

Dans un controller admin, j'ai une fonction ajax  qui doit être appelée  à partir d'un .tpl.

 

dans mon controller : 

public function ajaxProcessSearchAttributes()
    {
        echo Tools::jsonEncode(array('use_parent_structure' => false, 'data' => 'Hello World !'));
        die(Tools::jsonEncode('Hello World !'));
    }

Dans mon .tpl   :

<button id="nomdemonmodule_button">Press Here !</button>

{literal}
<script>
$('#nomdemonmodule_button').click(function(e) {
	$.ajax({
		type: 'POST',
		url: 'ajax-tab.php',
		dataType : 'json',
		data: {
			ajax: true,
			controller: 'nomdemoncontroller',
			action: 'SearchAttributes',
            token: '{/literal}{$token}{literal}'
		}
	})
	.done(function(data){
		 console.log(data);
	})
	.fail(function(err){
		 console.log(err);
	});
});
</script>
{/literal}

Le token (dans mon controller) : 

public function renderForm()
    {
        $this->context->smarty->assign(array(
            'token' => Tools::getAdminTokenLite('monmodulecontroller'),
            'ajax_url' => $this->context->link->getAdminLink('AdminModules'),
            'products' => Product::getProducts(Context::getContext()->language->id, 0, NULL,'id_product', 'ASC')
        ));
        $this->setTemplate('mon.tpl');
    }

Les problème que je rencontre est que la valeur retournée est toujours NULL.

 

Pourriez-vous me venir en aide ? 

 

D'avance un grand merci. 

 

Fox

 

Link to comment
Share on other sites

Salut Fox,

 

Si dans ton controller, tu overrides la fonction `postProcess()` Assure toi que la fonction parente est correctement appelée au début de ta fonction et pouvoir ainsi bénéficier de ses fonctionnalités.

 

<?
public function postProcess() 
{
    parent::postProcess();
    ...
}

Enjoy ! = )

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...