0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configLoader = $objectManager->get('\Magento\Backend\Model\UrlInterface');
$url = $configLoader->getUrl('testing/queue/purchase');
<script>
require([
 'jquery',
 'mage/mage',
 'mage/calendar'
], function($){
 $('#customer_company').on('change',function(event){
 $.ajax({
 url: '<?php echo $url ?>',
 type:'POST',
 showLoader: true,
 dataType:'json',
 complete: function(response) {
 console.log(response);
 },
 error: function (xhr, status, errorThrown) {
 console.log('Error happens. Try again.');
 }
 });
 });
});

i write this code in magento admin on change the ajax request call and its hit my controller but in console the response is Invalid Form Key. Please refresh the page

temper
1,04413 silver badges45 bronze badges
asked Oct 1, 2019 at 12:04
1
  • Use this:- "<?php echo $this->getUrl('testing/queue/purchase'); ?>" Commented Oct 1, 2019 at 12:07

3 Answers 3

3

/etc/adminhtml/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
 <router id="admin">
 <route frontName="testing_queue" id="testing_queue">
 <module before="Magento_Backend" name="Vendor_Module"/>
 </route>
 </router>
</config>

in Phtml File

function ajax(){
 var name = document.getElementsByName("product[name]")[0].value;
 var type = document.getElementsByName("product[product_category_type]")[0].value;
 $.ajax({
 url: 'admin/testing_queue/customajax',
 showLoader: true,
 data: {
 id:<?php echo $id ?>,p:pincode
 },
 type: "POST",
 success: function(result){
 console.log(result);
 }
 });
}
answered Oct 1, 2019 at 17:05
1

instead of using object manager and URL interface just use $this->getUrl()

jQuery.ajax({
 url: "<?php echo $this->getUrl('testing/queue/purchase') ?>",
 type: "POST",
 success: function (res) {
 console.log(res);
 }
});

If above code still showing error of form key, then try GET instead of POST

answered Oct 1, 2019 at 12:09
1

All possible solutions are:-

The right way is, inject the UrlInterface in you model block or whatever class constructor

Then call the getUrl() function

class SomeClass extends \Some\Other\Class
{
 protected $_backendUrl;
 public function __construct(
 ...........
 ...........
 \Magento\Backend\Model\UrlInterface $backendUrl,
 ...........
 ) {
 $this->_backendUrl = $backendUrl;
 }
 public function someFunction()
 {
 $params = array('some'=>'url_parameters');
 $url = $this->_backendUrl->getUrl("testing/queue/purchase", $params);
 }
}

or

You can easily get Admin url By calling

$this->getUrl('testing/queue/purchase');

Please note that "Context" type of object is loaded in the $this object

answered Oct 1, 2019 at 12:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.