【问题标题】:Can't get require_once called by ajax on localhost to work无法让本地主机上的 ajax 调用 require_once 工作
【发布时间】: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


    【解决方案1】:

    使用__DIR__ 是要走的路。您应该详细说明该声明:

    require_once __DIR__ . 'app/config/settings.php';
    

    我猜您只是缺少/,因为__DIR__ 将为您提供一条没有尾随/ 的路径。应该是:

    require_once __DIR__ . '/app/config/settings.php';
    

    【讨论】:

    • 感谢您的回答,但有了这个,我从一开始就收到此错误:Warning : require_once(G:\xampp\htdocs\website.de\app\class\controller/app/class/model/contactForm.php): failed to open stream: No such file or directory in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line Fatal error : require_once(): Failed opening required 'G:\xampp\htdocs\website.de\app\class\controller/app/class/model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
    • 我猜这是因为需要的文件在子文件夹中。
    • 我留在__DIR__ 是要走的路。只要确保路径正确并且一切正常。 app文件夹和php文件在同一个文件夹吗?
    • 正如您在我的问题中看到的那样,我通过 ajax 调用的文件在 root->app->class->controller/contactForm.php 我通过 require_once 在此文件中调用的文件在 root->app->class->model/contactForm.php 和 root->app->config/settings.php
    • 抱歉,根据该信息构建路径是 1 级。(即使我妈妈也可以)。尝试一下,你会确认这一点。我只能说__DIR__ 是要走的路
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2013-04-12
    • 2014-06-06
    • 1970-01-01
    • 2013-06-14
    相关资源
    最近更新 更多