简体   繁体   English

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

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

I have a php page , send.php that is successfuly echoing out an array when I go to that page. 我有一个php页面,send.php在我转到该页面时成功回显了一个数组。 However When i try to use ajax on index.php to access that page there is no response. 但是,当我尝试在index.php上使用ajax访问该页面时,没有任何响应。

This the page I am accessing send.php 我正在访问的页面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);    
    }
}

This is my index page 这是我的索引页

<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>

If I go to send.php in my browser it works and the array is printed, but when I go to index.php and press the button the ajax request is made successfuly but nothing is sent back. 如果我在浏览器中转到send.php,它将正常工作并打印该数组,但是当我转到index.php并按下按钮时,ajax请求成功完成,但没有任何回传。

It does work if I comment out require_once('./mysqlDB.php'); 如果我将require_once('./ mysqlDB.php')注释掉,它确实可以工作。 in send.php 在send.php中

I am new to using classes in php and wansn't sure what exactly I needed to google to find an answer. 我是在php中使用类的新手,并且不确定要在Google上找到答案的确切条件。

I figured it out. 我想到了。 The problem was I had html code on my mysqlDB.php page. 问题是我的mysqlDB.php页面上有html代码。 Thanks for telling me to look into my file structure. 感谢您告诉我调查文件结构。

I suspect this is to do with your folder structure. 我怀疑这与您的文件夹结构有关。

You have used a relative path to include your database file. 您使用了相对路径来包含数据库文件。 It is better practice to use the full path, then you can call the script from any context without any problem. 更好的做法是使用完整路径,然后您可以从任何上下文中毫无问题地调用脚本。

I would suggest something like: 我建议类似的东西:

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

or you can define the root as a constant in a common config file: 或者您可以在通用配置文件中将根定义为常量:

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