首页 > 解决方案 > 通过 Ajax 运行 php 脚本

问题描述

我正在建立一个新网站。

我自己在一个 linode 节点中托管它。该站点已经启动并运行。

我安装了 nginx 运行在 80 端口,apache 运行在 8001 端口。

我的问题是,当我进行 AJAX 调用时,响应(responseText)是整个 php 代码。我不明白如何让它通过 Apache 运行。

除了将端口更改为不与 nginx 冲突之外,我还没有对 apache 进行任何配置。

安装了apache2,安装了nginx。

function getSuccessOutput() {
$.ajax({
    method: 'get',
    url:'test.php',
    complete: function (response) {
    console.log(response.responseText);
        $('#output').html(response.responseText);
    },
    error: function () {
        $('#output').html('Bummer: there was an error!');
    },
});
return false;

}

在控制台中,我得到了完整的 php 脚本。

PHP 以 echo $result 结束。如果我在服务器中使用 php -f 运行它,则结果正常。

我只需要responseText中的结果,稍后我会看到如何处理。

我不了解如何使代码在 apache 上运行,我知道它需要由某人解释,但我不知道该怎么做。

谢谢!

标签: javascriptphpapachenginx

解决方案


检查您的 php 文件,如果文件开头有一个 open 标记:

<?php

不要使用短打开标签,因为它只有在使用 php.ini 配置文件中的 short_open_tag 启用时才可用。

<?

推荐阅读