Disclaimer: I'm pretty new to Joomla and web-development, so I probably am making a dumb beginner mistake. So far I've worked through much of the MVC tutorial in the Joomla documentation, and I've read answers to the What is the proper way to make an AJAX call in component? question, but I'm still having trouble.
I am modifying an existing component (J2Store) so I'm not using com_ajax. I want a JavaScript script to be able to use a method in a subcontroller (com_j2store/controllers/products.php) to retrieve data. In my subcontroller class I have a dummy method for testing this out:
site/components/com_j2store/controllers/products.php
<?php
/**
* @package J2Store
* @copyright Copyright (c)2014-17 Ramesh Elamathi / J2Store.org
* @license GNU GPL v3 or later
*/
// No direct access to this file
use Joomla\Registry\Format\Json;
defined('_JEXEC') or die;
require_once(JPATH_ADMINISTRATOR.'/components/com_j2store/controllers/productbase.php');
class J2StoreControllerProducts extends J2StoreControllerProductsBase
{
...
// My method
public function doSomething(){
echo "something";
}
}
Based on what I've read I cobbled together the following AJAX call:
site/templates/my_template/js/user.js
// menu select options
jQuery(document).ready(function() {
...
jQuery.ajax({
url: "index.php?option=com_j2store&format=raw&task=products.doSomething",
type: "GET",
success: function(response){
alert(response);
}
});
...
}
(This is a project I inherited and the previous developer made the choice to create and put code in the user.js template file. I am putting my code in this file as well because I'm modifying what he did.)
What I expect to happen when I trigger the AJAX call is an alert window popping up and saying "something". However when the AJAX call is triggered the console logs a 403 (Forbidden) error, and the line number it gives is indeed the line number of the AJAX call.
Because of my inexperience and how many different aspects are involved in this problem it's hard for me to know what's causing the issue. In particular I don't know if I've structured my code incorrectly in the Joomla structure or if my AJAX call is just wrong. Hopefully someone can help clarify things for me.
update mixahlos pointed out that my old url "index.php?option=com_j2store&format=raw&controller=products&task=doSomething" was incorrect, but I'm still getting the error