【问题标题】:No response being sent from ajax request when using require使用 require 时没有从 ajax 请求发送响应
【发布时间】:2023-03-23 11:06:01
【问题描述】:

我有一个 php 页面 send.php,当我转到该页面时,它成功地回显了一个数组。但是,当我尝试在 index.php 上使用 ajax 来访问该页面时,没有响应。

这是我正在访问的页面 发送.php

error_reporting(E_ALL);
ini_set('display_errors', 1);
$json = array(
    "test" => null,
    "success" => null,
    "urls" => null,
); 
$json = array("hi" => null, "success" => null);
$class = new test();
$json["success"] = true;
$json["hi"] = $class->_array;   
echo json_encode($json);


class test{
    public $_array = array();
    public $urlPreArray;    
    public $sensoredArray;

    function __construct(){
        require_once('./mysqlDB.php'); //this causes nothing to be sent back
        $cookieString = $_COOKIE['crUrl'];
        $this->_array = explode(",", $cookieString);    
    }
}

这是我的索引页

<html>
<head>
    <script src="jquery1.11.js"></script>
    <meta charset="utf-8">
    <script> 
    function load(){    
        $.get("./php/send.php",
        {
             action:"displayUrlsNotes"
        },
        function(data, status){
            console.log(status);
            console.log(data);
        }, "json");
        console.log("end");
    }
    </script>
</head>
<body>
    <button onclick='load();'>Click Me</button>
</body>
</html>

如果我在浏览器中访问 send.php,它可以工作并打印数组,但是当我访问 index.php 并按下按钮时,ajax 请求已成功发出,但没有发回任何内容。

如果我注释掉 require_once('./mysqlDB.php'); 它确实有效在send.php中

我是在 php 中使用类的新手,我不确定我究竟需要什么来谷歌找到答案。

【问题讨论】:

  • 您的 mysqlDB.php 在文件夹中还是在 index.php 路径中?
  • 我有 index.php 和一个名为 php 的文件夹。 mysqlDB.php 和 send.php 在 php 文件夹中。我不相信这是一个目录问题,因为我将 mysqlDB.php 放在与 index.php 相同的文件夹中,但这并没有解决它。另外,如果这是问题所在,我认为在 send.php 时会出错

标签: php jquery ajax header require


【解决方案1】:

我想通了。问题是我的 mysqlDB.php 页面上有 html 代码。感谢您告诉我查看我的文件结构。

【讨论】:

    【解决方案2】:

    我怀疑这与您的文件夹结构有关。

    您已使用相对路径来包含您的数据库文件。最好使用完整路径,这样您就可以从任何上下文调用脚本而不会出现任何问题。

    我建议如下:

    require_once($_SERVER['DOCUMENT_ROOT'].'/mysqlDB.php'); 
    

    或者您可以在通用配置文件中将根定义为常量:

    define('ROOT_DIR', '/home/mysite/public_html');
    require_once(ROOT_DIR.'/mysqlDB.php');
    

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2018-05-04
      • 1970-01-01
      • 2015-11-16
      • 2018-05-01
      • 2013-03-02
      • 1970-01-01
      • 2014-01-12
      • 2017-09-24
      相关资源
      最近更新 更多