Trying to work out how to access custom database with ajax

Found a tutorial for custom database
Know javascript fetch.
Trying to find endpoints to get,post etc

There are different approaches.

  • You could just use a resource with an empty template, set the content type to JSON and then run a snippet in the content to handle the processing.
  • Or you could use the extra QuickApi.
  • Or develop a RESTful API.

The best approach for your case depends on your specific requirements and programming skills.

If I wanted to change this into an extra, which would be the best approach?
I just need to know the best way to use javascript to add,push and delete data from the database.
I would like to try and develop a commenting extra later on.
My programming skills are in javascript and css.
I do need to learn php, but I will need to learn as I go along.
I did learn visual basic a long time ago.
I also have posted an issue with this tutorial
https://docs.modx.com/3.x/en/extending-modx/tutorials/using-custom-database-tables#
This is what I posted:
I get an error when trying to add namespace , Modx is running in Xampp.
The path in modx to xpdo is supposed to be core/vendor/xpdo/src/xPDO/ ,but my path is core/vendor/xpdo/xpdo/src/xPDO/
xpdo is 2 directories?

This is on modx3.0.1

Also, where do I put the project1 directory?
I have put it in htdocs/modx3/

You can’t access the database with javascript directly. PHP is used to interact with the database. So you need to learn at least some PHP for this.

Maybe instead of using the tutorial you linked, try using the extra ExtraBuilder. It automates a lot of the steps in the tutorial.

If you want to program your own extra, a good way to start is to read the code of similar extras to get familiar with the way it works in MODX.

MODX extras often use processors as AJAX-endpoints (especially for request from the manager) so take a look at it.

1 Like

I will use the ExtraBuilder for now.
But is there a error in the tutorial?
I will have a look at an extra that uses endpoints.
Is this the only way ajax works, through endpoints?
Thanks for the help.

Basically, yes, you basically make requests from your FE (front end) using any kind of technology (axios, , ajax_xmlhttprequest, etc) to a BE (back end) in this case MODx, as mentioned before, for the database part, its better to use the extraBuilder, and focus on the “Building API Endpoints” section of the tutorial, you’ll end up with a simple service rest layer (endpoint) that you can run GETs or any other valid http operation.

For simple CRUD operations, you should be able to go around without having to write much code, just build the package, create the table, and create the rest class

Can you tell me where to creat the rest folder?
I am following this tutorial developing restful api

Create the “rest” folder in the webroot.

As you are using MODX 3:
I don’t think the tutorial has been adapted to MODX 3 and there is also a bug, that isn’t fixed in the current version 3.0.1. So take a look at this topic

And if you have problems with the .htaccess file from the tutorial, try this instead:

1 Like

I would need to change the file paths in the index.php file in the rest folder to my directory name?

// Boot up any service classes or packages (models) you will need
$path = $modx->getOption('nutritionlog.core_path', null,
   $modx->getOption('core_path').'components/nutritionlog/') . 'model/nutritionlog/';
$modx->getService('nutritionlog', 'nutritionlog', $path);

Also with the endpiont what is class and classkey?

When you parse your schema with the extra ExtraBuilder in MODX 3, it create a file bootstrap.php in core/components/name_of_your_extra that already calls $modx->addPackage(...) to load your custom classes. So I don’t think this code ($modx->getService(...)) is necessary in MODX 3 anymore.

Also I think $modx->getService() is deprecated in MODX 3 and replaced by a Dependency Injection Container.


The name of the class is the “controllerClassPrefix” you defined (‘MyController’ in the example) followed by the endpoint name. So the content of the class MyControllerItems is executed when the url https://yourdomain.com/rest/items is called.

class MyControllerItems extends modRestController {
    public $classKey = 'ToDoItem';
    ...
}

The $classKey defines from which of your classes (from the schema) the data is queried.