简体   繁体   中英

Jquery ajax call a php script with require once

I have an ajax request like :

$.ajax({
    type: "GET",
    url: "services/Test.class.php",
    data: "call=getTest",
    success: function (data) {
        alert(data);
    },
    error: function (response) {
        alert("Error getting php file");
    }
});

So, in my class ( Test.class.php ), I have a getTest() function and a require_once('OtherPHP') ,

When I test this code, I have an error in require once :

No such file or directory

in my alert(data) how can I fix it?

It seems like you've included a wrong path of OtherPHP file in Test.class.php. Use file_exists() function to make sure that given path really exists before including/requiring in Test.class.php

if (file_exists(`OtherPHP.php`)) {
    require_once(`OtherPHP.php`)
} else {
    echo "The file `OtherPHP.php` does not exist";
}

You cant able to call the class directly from ajax,

  • create new php file say test.php

in test.php

   include("Test.class.php");    
   $test = new Test();
   $test ->getTest(); //print the getTest outpout here 

Javascript:

     $.ajax({
        type: "GET",
        url: "test.php",

        .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM