You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

线上PHP服务器请求backend.php返回HTML的问题求助

线上服务器请求backend.php返回login/index.php页面的问题排查

本地XAMPP环境运行正常,但部署到线上服务器后,login/index.php中的AJAX请求../backend.php时,返回的却是login/index.php的HTML代码。即使将backend.php简化为仅输出调试信息的版本,问题依旧存在。

项目结构如下:

login
  |__ index.php

backend.php

相关代码展示

domain/url/login/index.php

<?php
    session_start();
    if (isset($_SESSION['empno'])) {
        header("Location: ../homepage/index.php");
        exit();
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        function preventBack() {
            window.history.forward();
        }
    
        setTimeout(preventBack(), 0);
        window.onunload = function() {
            null
        };
    </script>
    <title>My website</title>
    <style>....</style>
</head>
<body>

    <!--some HTML code here-->

    <script type="text/javascript">
        let xhr = new XMLHttpRequest();
        xhr.open("POST", "../backend.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onload = function () {
            if (xhr.status === 200) {
                console.log(xhr.responseText);
            }
        };
        let data = "input=get_data";
        xhr.send(data);
     </script>
</body>
</html>

domain/url/backend.php(原代码)

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    header('Content-Type: text/plain; charset=utf-8');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type');
    header('Access-Control-Allow-Credentials: true');

    require "db_particulars.php";

    $con=mysqli_connect($server,$username,$pass);
    $connection=mysqli_select_db($con,$database);

    if(!$connection){
        echo "Connection to database failed! Please try again";
    }
    else{
        if ($_SERVER['REQUEST_METHOD'] === 'POST'){
            if ($_POST["input"] == "get_data"){
                $sql = "SELECT * FROM `employee_master` GROUP BY `Department` ORDER BY `Department`";
                $result = $con->query($sql);
                if($result->num_rows > 0){
                    $final = array();
                    while($row = $result->fetch_assoc()) {
                        array_push($final,$row["Department"]);
                    }
                    echo json_encode($final);
                }
                else
                    echo "No data found";
            }
        }
    }
?>

简化调试版backend.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Output debug information
echo "Debug Information:\n";
echo "Request Method: " . $_SERVER['REQUEST_METHOD'] . "\n";
echo "POST Data: " . print_r($_POST, true) . "\n";

// Set appropriate headers
header('Content-Type: text/plain; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Credentials: true');
?>

可能的原因及解决方案

  1. 服务器重写规则(.htaccess)冲突

    • 线上服务器的.htaccess可能配置了强制跳转规则,误将backend.php的请求重定向到login/index.php。
    • 解决:检查.htaccess文件,添加例外规则允许直接访问backend.php,示例:
      RewriteEngine On
      # 排除backend.php的重定向
      RewriteCond %{REQUEST_URI} !^/backend.php$
      # 原有重写规则...
      
  2. 文件权限配置错误

    • backend.php的权限设置不当,导致服务器无法执行该脚本,转而返回默认页面。
    • 解决:将backend.php权限设为644,所在文件夹权限设为755,确保服务器有读取和执行权限。
  3. 路径解析错误

    • AJAX中的相对路径../backend.php在服务器环境下解析错误,请求了不存在的路径,触发服务器默认页面返回。
    • 解决:改用绝对路径发起请求,示例:
      xhr.open("POST", "/url/backend.php", true);
      
      或通过window.location.origin拼接完整路径,确保指向正确的脚本文件。
  4. 服务器安全模块拦截

    • 线上服务器的安全模块(如ModSecurity)可能拦截了对backend.php的POST请求,强制跳转到登录页面。
    • 解决:查看服务器安全日志确认是否有拦截记录,添加规则允许对backend.php的合法请求。
  5. 默认文档优先级问题

    • 服务器虚拟主机配置中,默认文档的优先级设置可能导致请求被错误解析为login/index.php。
    • 解决:直接访问domain/url/backend.php验证脚本是否能正常响应;检查虚拟主机配置,确保PHP文件能被优先解析。

内容的提问来源于stack exchange,提问作者Abhishek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 17:37:06