douju1365 2018-09-06 21:50
浏览 163
已采纳

xmlHttp responseText返回php文件而不是运行php脚本的结果

I have the following programs, test.html and test.php

test.html:

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
</head>

<body>
<div id="root"></div>
<script type="text/babel">


class NameForm extends React.Component {


    componentDidMount(){
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                console.log(xmlHttp.responseText)
            }
        }.bind(this)

        xmlHttp.open("GET","test.php",true)
        xmlHttp.send(null)
        event.preventDefault()
    }


    render() {
        return (<div/>)
    }

}

ReactDOM.render(
    <NameForm />,
    document.getElementById('root')
);


</script>
</body>

test.php

<?php
echo "this is a test"
?>

When I open the html page I get the content of the php script written in the console rather than the result of running the php script as I was expecting, that is to say, in the console I get this:

<?php
echo "this is a test"
?>

rather than this:

this is a test

Why is this? How can I fix it? Thanks,

Also in case this is relavent I am running this locally using python SimpleHTTPServer

  • 写回答

1条回答 默认 最新

  • duanchou6534 2018-09-06 22:18
    关注

    PHP is a server sided language so your browser can not parse it unlike html , Javascript or ... that your browser can run them.

    So you need a server to run PHP. (Installing PHP)

    On windows you can use Xampp or Wamp to have it locally easily and run your codes. And make your computer your server to test your codes...

    Another way is to install PHP manually and run it.

    But if you want to have real server and host sites, it is more complicated ...

    PS:if you want to just test your codes without change your computers configuration there are free hostings you can use them ...

    You can read more at : What you need to get started with PHP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 对语音信号进行变调时,间接改变时序从而实现语音变速,进而将变调与变速同时实现、参数合成法换为波形合成法
  • ¥15 Python实现hog特征图可视化
  • ¥30 德飞莱51单片机实现C4炸弹
  • ¥50 CrossLink-LIF-MD6000 型 FPGA 的 CMOS 转 MIPI D-PHY IP 核功能使用异常
  • ¥15 proteus控制16x16LED点阵显示屏的设计
  • ¥30 求会做山景bp1048b2程序的。做直播声卡用
  • ¥15 求数学建模论文问题指导
  • ¥15 51单片机与数码管实现电子琴