Magento 2 jquery ajax request

Magento 2 jquery ajax request

Today topic Magento 2 jquery ajax request. sometime we need to load data with loading the page so for that we need ajax to load data. So today we focus how to use jquery ajax request in magento 2. Magneto have already added the jquery can inject it with require. Here is example of using of jqery ajax for getting the json response from controller.


require(['jquery', 'jquery/ui'], function($){
          method: "POST",
          url: "<?php  echo $block->getUrl('helloworld/ajax/index'); ?>",
          data: { q: "test"},
          dataType: "json"
      .done(function( msg ) {
        //do something with you return data


you we use controller ResultFactory to get back json response.

namespace QaisarSatti\HelloWorld\Controller\Ajax;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class Index extends Action {
protected $request;
public function __construct(Context $context,array $data = [])
public function execute() {
    $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
    return $resultJson;



Recent Blogs





One thought on “Magento 2 jquery ajax request

Leave a Reply