Magento 2 – Troubleshooting AJAX Request Failures

adminajaxhttp-requestmagento2magento2-dev-beta

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?

Best Answer

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"}
});
Related Topic