Jump to content

Ajax call from my module returns php file


prak

Recommended Posts

Hello, I am trying to call a php function on clicking a button in my module. I have added the ajax-call.php in the root folder of my module. Here is my code :

 

jquery to call the function

  $('.target').unbind('click').click(function(){
       $.ajax({
        url: '{$base_dir}modules/customizeproducts/ajax-call.php',
        type: 'get',
        data:'ajax=true',
        success: function(data) {
        console.log(data);
        }
      });
      return false;
});

ajax-call.php

<? php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');

public function call(){
  return "called from ajax-call.php";
}
if ($_GET['ajax']){
   echo function call();
}
?>

console.log(data) returns the whole file, how can I get the value returned by the call() function?

Link to comment
Share on other sites

Hi !

 

try this :

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');

function call(){
    return "called from ajax-call.php";
}

if (Tools::isSubmit('ajax')){
    echo call();
}

Note that you have a white space in php aperture (< ?    php).

Perhaps you should replace the echo for die too.

Edited by Gabriel Perez (see edit history)
  • Like 1
Link to comment
Share on other sites

show us your new code.  the issue is likely the space on line 1

<? php

also, checked your php error log for the actual error behind the 500

It seems that the space was the issue, here is my code now:

 

ajax-call.php

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');

public function call(){
  return "called from ajax-call.php";
}
if (Tools::isSubmit('ajax')){
    echo call();
}

I checked the log and the reason for 500 error is "server reached pm.max_children setting (5), consider raising it", I found that the value of pm.max_children was set to 5 in www.conf, what value should I give it?

Link to comment
Share on other sites

 

Cambia el debug para saber cual es el error 500 :

 

Archivo /confi/defines.inc.php :

/* Debug only */
if (!defined('_PS_MODE_DEV_')) {
    define('_PS_MODE_DEV_', true);
}

I had already set _PS_MODE_DEV_ to true, I think I just have to raise the pm.max_children

Link to comment
Share on other sites

I have changed these values in my www.conf file, php.max_children to 500, php.max_requests to 10000, pm.min_spare_servers to 20 and pm.max_spare_servers to 100 and restarted php7.0-fpm. Now I get the same error message in the console but nothing in the log.

 

error message in the console:

 

'GET http://localhost/modules/customizeproducts/ajax-call.php?ajax=true 500 (Internal Server Error)
 

XHR finished loading: GET "http://localhost/modules/customizeproducts/ajax-call.php?ajax=true".'

Link to comment
Share on other sites

Well, I tried this and it is working

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');

function call(){
  echo "called from ajax-call.php";
}
if($_GET['ajax']){
    call();
}
?>

Thank you both very much for your replies

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...