Javascript Jquery ajax使用require一次性调用php脚本
我有一个ajax请求,如:Javascript Jquery ajax使用require一次性调用php脚本,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个ajax请求,如: $.ajax({ type: "GET", url: "services/Test.class.php", data: "call=getTest", success: function (data) { alert(data); }, error: function (response) { alert("Error getting php file"); } }); 因此,在
$.ajax({
type: "GET",
url: "services/Test.class.php",
data: "call=getTest",
success: function (data) {
alert(data);
},
error: function (response) {
alert("Error getting php file");
}
});
因此,在我的类(Test.class.php)中,我有一个getTest()
函数和一个require\u once('OtherPHP')
测试此代码时,我在require once中有一个错误:
没有这样的文件或目录
在我的警报(数据)
如何修复它?似乎您在Test.class.php中包含了一个错误的
OtherPHP
文件路径。使用file\u exists()
函数在Test.class.php
if (file_exists(`OtherPHP.php`)) {
require_once(`OtherPHP.php`)
} else {
echo "The file `OtherPHP.php` does not exist";
}
您无法直接从ajax调用该类
- 创建新的php文件say test.php
include("Test.class.php");
$test = new Test();
$test ->getTest(); //print the getTest outpout here
Javascript:
$.ajax({
type: "GET",
url: "test.php",
.....
不,我的错误在Test.class.php中,所以我认为我有一个好的路径。其他php文件在哪里?@Felix指的是另一个所需文件的位置(路径)。只要试着用浏览器直接调用“services/Test.class.php”就行了。我正在用另一种方式使用Test.class.php,这没关系,所以我认为我的路径没有问题:)你也可以发布相关的php代码吗?