【发布时间】:2013-12-16 03:02:57
【问题描述】:
我快要疯了。
我在本地有一个简单的网站,其中包含一个联系表单功能,仅适用于 PHP。
现在我想通过 AJAX 调用它,但由于相对路径,我总是会出错。
问题是,ajax调用的php文件有两个require_once,那就不行了。
我知道我应该使用绝对路径来让它工作,我尝试了不同帖子建议的不同方法,但都会导致错误。
我尝试过使用
require_once __DIR__ . 'app/config/settings.php';
我在 index.php 中使用了define('ROOT_SYS',dirname(__FILE__).'/'); 进行 ajax 调用,并在 php 文件中有如下要求:
require_once ROOT_SYS . 'class/model/contactForm.php';
以及许多其他方法,例如此问题PHP include best practices question 中推荐的方法,但均未成功。我让它们都在 php 端工作,但是当我使用 ajax 时,它总是会导致如下错误:
注意:使用未定义的常量 ROOT_SYS - 在 G:\xampp\htdocs\website.de\app\class\controller\contactForm.php 中假定为“ROOT_SYS”第 6 行
警告:require_once(ROOT_SYSclass/model/contactForm.php):无法打开流: 中没有这样的文件或目录G:\xampp\htdocs\website.de\app\class\controller\contactForm.php 第 6 行
致命错误: require_once():在 G:\xampp\htdocs\website.de\app\class 中打开所需的 'ROOT_SYSclass/model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') 失败\controller\contactForm.php 第 6 行
或
警告:require_once(app/class/model/contactForm.php):打开流失败:G:\xampp\htdocs\website.de\ 中没有这样的文件或目录app\class\controller\contactForm.php 第 6 行
致命错误:require_once(): Failed opening required 'app/class /model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') 在 G:\xampp\htdocs\website.de\app\class\controller\contactForm.php 上线 6
常量未定义,因为它未包含在内,或者打开流失败。
我的意思是必须有办法让它在调用 PHP 文件中使用 require_once。
你有什么经验可以和我分享吗?我现在真的很绝望..:(
我的文件夹结构是这样的:
app
-class
--controller
--helper
--model
--view
-config
-template
docroot
lib
system
-css
-js
index.php
我在本地主机上的项目路径:
G:\xampp\htdocs\website.de
http://localhost/website.de/
我的 ajax 调用
$.ajax({
type: "POST",
url: "app/class/controller/contactForm.php",
data: content
})
以及调用contactForm.php中的要求我是如何从一开始就拥有它们的。
require_once 'app/class/model/contactForm.php';
require_once 'app/config/settings.php';
必须有某种方法让它工作,最好是在本地和网络服务器上工作的设置。
我知道要阅读的内容很多,所以感谢您抽出宝贵时间!
【问题讨论】:
标签: php ajax localhost absolute-path require-once