Hello,
Before oxid I was working with mvc php framework where when you had to make an ajax call, you simply had to create the ‘calling’ part in jquery where in url you had to define controllers name and its function. e.g. example.com/mycontroller/ajax_function. And then you should have the Mycontroller.php file with ajax_function() in it.
In oxid eshop, in my module I have created a controller MyController with a function test().
Whithin the js file I make a simple post request on url: '/index.php?lang=1&cl=MyController&fnc=test'.
It didn’t work.
Then I tryed to extend and existing controller wich was ShopControl with test() function within it and changed appropriately url in .js file. but it also din’t work.
Both attempions having as result Status Code: 302 Found woth Header field Location: http://oxid.loc/index.php?lang=1&cl=start&redirected=1
Can somebody explain which is the logic? What didn’t I undestand?
url: '/index.php?lang=1&cl=MyController&fnc=test' is the right way to go,
but “it didn’t work” is not a proper problem description. Check logs for some more exact and meaningful error messages.
Ok, but I don’t receive any error, either exception. log file stays unchanged. I’ve updated the url as you shown, the status code of request is now 200, seems to be ok, but still something is wrong. As responce I recieve html of home page. Nothing relative to this problem in apache log files too. I added die(‘request died’); in test(), but it still continues returning the home page.
Thank you for responce. General:
Finally an exception apeared. It says EXCEPTION_SYSTEMCOMPONENT_CLASSNOTFOUND mycontroller ["[object] (OxidEsales\\Eshop\\Core\\Exception\\SystemComponentException(code: 0):
By the way, I have multiple times composer dump-autoload executed. Exception still being thrown.
$.ajax({
type: "POST",
url: '/index.php?lang=1&cl=MyController&fnc=ajax_func',
success: function (response) {
if (response.length > 20) {
console.log('there is a problem CASE 1');
}
else {
console.log(response);
}
},
error: function () {
console.log('there is a problem CASE 2');
}
});
<?php
namespace Imoleg\Toaster\Controller;
class MyController
{
const MESSAGE = 'It is a response';
public function ajax_func()
{
echo json_encode('here I am.');
}
}
Why is metadata so scrambled? In “autoload” should be the base namespace wihtout “Controller”. In your metadata you are not defining the controller, so Oxid cannot know it.
Probably you are right. But what you mean to register my controller in metadata? It is not extending any Namespaced controller. Should I put it within ‘controllers’ var and not in ‘extend’? If so, does the key value matters? e.g. ‘modulscontroller’ => Imoleg\Toaster\Controller\MyController::class?
Thank you for help!
*Metadata file is not scrambled, simply the editor of this forum has replaced the ‘=’ with ‘=&g;’
Have registered the controller as you said:
metadata.php: 'immycontroller' = Imoleg\Toaster\Controller\MyController::class
file.js: url: '/index.php?lang=1&cl=immycontroller&fnc=ajax_func',
composer.json: "Imoleg\\Toaster\\": "./source/modules/imoleg/imToaster"
I don’t mean to resurrect an old form post. The forum seems pretty inactive nowadays anyway.
Does anyone know how to avoid a 302 redirect for an admin AJAX call? when I make a request as outlined here, it still makes a 302 redirect and I am not sure how I can pass an authenticated request to the controller.
This post and another one kept popping up as a solution from Googling - but the answers are not given which can be frustrating for others trying to find the similar resolutions.