繁体   English   中英

使用require时,没有从ajax请求发送响应

[英]No response being sent from ajax request when using require

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

我正在访问的页面send.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中使用类的新手,并且不确定要在Google上找到答案的确切条件。

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

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

您使用了相对路径来包含数据库文件。 更好的做法是使用完整路径,然后您可以从任何上下文中毫无问题地调用脚本。

我建议类似的东西:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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