首页 > 解决方案 > Ajax 返回 PHP 文本而不是解析代码

问题描述

本地环境为:Apache/2.4.34 (Unix) PHP/7.1.23 all conf。文件已经过检查和重新检查,并添加了额外的 cmd,例如:


        <IfModule>
       # PHP 7 specific configuration
       ....
       </IfModule>
        phpinfo() works fine etc.

xmlhttp 代码是:

var xmlhttp = new XMLHttpRequest();

          xmlhttp.open("GET", "php/getARCHIVE.php");
          xmlhttp.setRequestHeader('Access-Control-Allow-Origin','localhost');
          xmlhttp.send();

          xmlhttp.onreadystatechange = function() {
            if (this.readyState === 4 && this.status === 200) {
              console.log(this.responseText);
            } else {
             console.log("Loading...");
            };
          }
            }

并返回:加载中...

    <?php
    echo 'hi everyone!';
    ?> 

作为文本!

或者 ......

    $.ajax( {
        url: 'php/getArchive.php',
            crossDomain: true,
        error:      function( jqXHR, statusMessage, errorThrown){console.log('serverRequest for php:' + "\n" + statusMessage + "\n" + errorThrown);},
        success:    function( response, status, jqXHR ){
            callback(response);
        }
    });
    }

...返回相同的文本:

   serverRequest for php:
    parsererror
    Error: Invalid XML: <?php
    echo 'hi everyone!';
    ?>

随附的注释/错误是:“XML 解析错误:未找到根元素”

那么如何让服务器识别 .php 文件 - 这只是:

     <?php echo 'hi everyone!'; ?>

i.e. the text that is being returned

标签: phpajaxxmlhttprequestapache2.4

解决方案


推荐阅读