Jump to content

AJAX call to controller returns error 500.


Recommended Posts

Hey everyone,

I'm busy adding in a module on my shop for some external validation tools; but when trying to call the controller I keep getting error 500 from the server on my AJAX call.

It's my first time developping a module for prestashop and my first time doing php so it's a bit... frustrating.

 

I've got following files:

- modules/mymod/WCPSSO.php

 this actually doesn't contain much, only some information.

- modules/WCPSSO/controllers/front/sso.php

this is where my problem arrises; if I try to use javascript to call this php file in an ajax request i get a error 500.

code inside is:

<?php
   
     /**
    * <ModuleClassName> => WCPSSO
    * <FileName> => sso.php
    * Format expected: <ModuleClassName><FileName>ModuleFrontController
    */
    
    class CustomSsoModuleFrontController extends ModuleFrontController{
        
        public function init(){
            $this->ajax = true;
        }
        
        public function initContent(){
            if ($this->context->customer->isLogged()) {
            
            $theCurrentUsersId = (int)$this->context->cookie->id_customer;
	    
            $sql = new DbQuery();
            $sql->select('SSO_key');
            $sql->from('wcpsso');
            $sql->where('id_customer ='.$theCurrentUsersId);
            $getAcceptanceKey = Db::getInstance()->executeS($sql);
            
            
            if($getAcceptanceKey == null){
                echo 'not found';
            } else {
                //$this->context->smarty->assign('SSO_ACCESS_KEY', $getAcceptanceKey[0]);
                //call via: "./sso.php" ? 
                //redirect goes here; called by: ./sso.php
                
                echo $getAcceptanceKey[0];

            }
            
            
            } else {
                //die
            }
            
        }
       
    }
   
?>

Ajax call:

function ajaxTest(){
    const xhttp = new XMLHttpRequest();
    
    xhttp.onload = function() {
        if(xhttp.readyState == 4 && xhttp.status == 200){
            console.log(xhttp.responseText);
        }
    }
    
  xhttp.open("GET", "./modules/WCPSSO/controllers/front/sso.php");
  xhttp.send();
  
}

I tried the last also with ? & ?ajax=true ; to no effect.

 

Anyone an idea? I've read the documentation, but it's confusing me.

Link to comment
Share on other sites

I'll share my code as an ajax example.

modules/productdetailspdf/controllers/front/DisplayDownloadButton.php

class productdetailspdfDisplayDownloadButtonModuleFrontController extends ModuleFrontController
{
    public $auth = false;
    public $ajax;

    public function display()
    {
        $this->ajax = 1;

        // if (php_sapi_name() !== 'cli') {
        //     $this->ajaxDie('Forbidden call.');
        // }

        // Additional token checks

        // ...
        $id_product = Tools::getValue('id_product');
        $product = new Product((int) $id_product);
        $custom_object = $product->description[$this->context->language->id];
        $pdf = new PDF($custom_object, 'CustomPdf', Context::getContext()->smarty);
        try{
            $this->ajaxDie($pdf->render());
        }catch(Exception $e){
            $this->ajaxDie($e->getMessage());
        }

        // $this->ajaxDie("hello\n");
    }

    
}

And below is ajax call

function generatePDF() {
    window.location.href='/index.php?fc=module&module=productdetailspdf&controller=DisplayDownloadButton&id_product='+pdfProductId;
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...