title: Magento 2 proper way to do ajax call?
body: <p>This is my controller:</p>
  
  <p>namespace Mvp\Mercury\Controller\Index;</p>
  
  <p>
  
  <pre><code>class Display extends \Magento\Framework\App\Action\Action
  {
  
  protected $resultJsonFactory;
  
  public function __construct(
      Context                                             $context,
      \Magento\Framework\Controller\Result\JsonFactory    $resultJsonFactory
  ) {
  
      $this-&gt;resultJsonFactory            = $resultJsonFactory;
      parent::__construct($context);
  }
  
  
  public function execute()
  {
      $result                 = $this-&gt;resultJsonFactory-&gt;create();
      if ($this-&gt;getRequest()-&gt;isAjax())) 
      {
          $test=Array
          (
              'Firstname' =&gt; 'What is your firstname',
              'Email' =&gt; 'What is your emailId',
              'Lastname' =&gt; 'What is your lastname',
              'Country' =&gt; 'Your Country'
          );
          return $result-&gt;setData($test);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I´m making a ajax call to this controller however gives me error , i´m trying it this way:</p>
  
  <pre><code>function addItemToShoppingCart(data)
  {
  console.log("You are about to add part: " + data.productNumber + " with qty: " + data.quantity + " and price: " + data.price);
  
  var sku        = data.productNumber,
     qty        = data.quantity,
     price      = data.price,
     base_url   = '&lt;?php echo $this-&gt;getUrl(); ?&gt;',
     module_url = 'helloworld/index/display',
     full_url   = base_url + module_url;
  
  var cart_info = {
      sku : sku,
      qty : qty,
      price: price
  };
  
  console.log(full_url);
  console.log(cart_info);
  
  $.ajax({
      showLoader: true,
      url: full_url,
      data: cart_info,
      type: "POST",
      dataType: 'json'
  }).done(function (data) {
  
      console.log('success');
  
  });
  } 
  </code></pre>
  
  <p>based on that i don´t know why it gives me error, the exact code it gives is 500, i checked permissions and they have everything, so what could be the problem here?</p>