2

I made an ajax request (in the admin html) and got the following response:

"{"error":true,"message":"You entered an invalid Secret Key. Please refresh the page."}"

new Ajax.Request('getAjaxExportUrl() ?>', { method: 'get', parameters: { "store" : "someStore" }

.

public function getAjaxExportUrl() { return $this->getUrl('someURL'); }

What's wrong?

Omry Zobel
  • 517
  • 1
  • 7
  • 11

1 Answers1

17

Magento is using secure key validation in admin aria.

Simplest way to use secure key is use POST and add form_key to data. Example:

jQuery.ajax( {
    url: 'http://mage.dev/admin/vendor/module/validateTest',
    data: {form_key: window.FORM_KEY},
    type: 'POST'
}).done(function(a) { 
    console.log(a); // log: {a: "b"}
});
KAndy
  • 20,811
  • 3
  • 49
  • 59
  • Do you know why this fails ? http://magento.stackexchange.com/questions/103068/magento2-ajax-fails-in-admin – Lachezar Raychev Feb 23 '16 at 16:01
  • May be your admin URL doesn't have secure key in it. http://magento.stackexchange.com/questions/104069/magento2-how-to-generate-admin-url-with-secure-key-from-a-controller – Mukesh Chapagain May 08 '16 at 08:26