javascript - 从 AJAX 响应程序脚本调用 PHP 类

标签 javascript php jquery ajax class

我正在尝试通过 AJAX 访问 PHP 文件

当我使用像这样的简单 PHP 文件时:

<?php

header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');

$mode = $_POST['ajax_mode'];

switch ($mode) {
    case "test":
        if (isset($_POST['...'])) {
            $test = $_POST['...'];
            echo $test;
        }
        break;
}
?>

一切正常。但如果我尝试使用其他类,例如 sql 类,则会出现内部服务器错误。

这是我的 AJAX 脚本:

function ajax(test, test1)
    {
        if (typeof test!== "undefined")
        {
            test_set = true;
            if (window.XMLHttpRequest) {
                request = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                try {
                    request = new ActiveXObject('Msxml2.XMLHTTP');
                } catch (e) {
                    try {
                        request = new ActiveXObject('Microsoft.XMLHTTP');
                    } catch (e) {
                    }
                }
            }
            if (!request) {
                alert("No Chance!");
                return false;
            } else {
                var url = ".../.../.../ajax_responder.php";
                request.open('POST', url, true);
                request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                request.send('ajax_mode=test' + '&test=' + test);
                request.onreadystatechange = function() {
                    if (request.readyState === 4)
                    {
                        interpretRequest();
                    }
                };
            }
        }
    }

    function interpretRequest() {
        if (request.status != 200) {
            alert("Request-Error: " + request.statusText);
        } else {
            var content = request.responseText;
            var string_collection = content.split("_");
            alert(string_collection[0]);
        }
    }
});

编辑:这是使用类的服务器端脚本,在此版本中我收到内部服务器错误

<?php

include '../../sql.php';
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');
$mode = $_POST['ajax_mode'];
switch ($mode) {
    case "test":
        if (isset($_POST['...'])) {
            $ace = $_POST['...'];
            $sql = new sql_();
            echo $sql->execute();
        }
        break;
}
?>

最佳答案

我的建议和可能的答案 - 将 include 放在标题后面:

<?php

header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');

include '../../sql.php';

$mode = $_POST['ajax_mode'];
switch ($mode) {
    case "test":
        if (isset($_POST['...'])) {
            $ace = $_POST['...'];
            $sql = new sql_();
            echo $sql->execute();
        }
        break;
}
?>

如果您首先包含一个文件(可能会产生错误消息)然后发送 header ,则可能会导致问题。根据经验,PHP 中的 header 应始终在任何输出发生或可能发生之前设置(除非您使用输出缓冲,但这是另一个故事)。

如果这没有帮助 - 您是否已尝试完全省略标题?

关于javascript - 从 AJAX 响应程序脚本调用 PHP 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25659068/

相关文章:

php - 将产品添加到数据库时添加日期/时间

php - 排序 table 锯表加载而不仅仅是点击

jquery - 在 asp.net mvc4 应用程序中使用 Jquery 验证表单

javascript - jQuery Validate 不会一次显示所有错误消息,一次只显示一条

php - MySQL 如何连接两个不同数据库中的两个表?

javascript - 立即函数调用语法

javascript - 根据鼠标位置添加 Css 类

jquery - 无法在jsp中创建jtable

javascript - FullCalendar - 默认事件持续时间

javascript - 如何将 ng-template 分配给指令 templateUrl 字段