![](/img/trans.png)
[英]How use image in api get by axios request in html tags in ReactJS?
[英]reactjs html get request not received by php
我编写了一个网页,当按下该网页时,该网页将一个get请求发送到PHP脚本。 PHP脚本创建一个名为asdf.txt的文件。 目前,我在2个不同的服务器上都有此代码,其中之一是Heroku。 在Heroku上,此代码可以正常工作:创建了一个名为asdf.txt的新文件。 但是,在另一台服务器(我要在上面放置此代码的服务器)上,这不起作用。 但是,HTML页面上没有错误消息。
理想情况下,我希望对如何进行调试提出一些建议。 如果有什么具体的想法请告诉我,但是我也对解决这个问题的任何方式都感兴趣。 谢谢你的时间。
这是PHP
<?php
system('echo asdf > asdf.txt', $retval);
?>
这是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 {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this)
}
handleSubmit(){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
}.bind(this)
xmlHttp.open("GET","test.php",true)
xmlHttp.send(null)
event.preventDefault()
}
render(){
return (
<div style={{fontSize: 30}}>
<form onSubmit={this.handleSubmit}><input style={{fontSize: 30}} type="submit" value="Submit"/></form>
</div>
)
}
}
ReactDOM.render(
<NameForm />,
document.getElementById('root')
);
</script>
</body>
在PHP中,有时system()
, exec()
和他们的朋友没有给我们任何错误消息。 甚至我们将display_erros
设置为On
。 作为一种解决方法,我建议您创建自定义帮助程序函数,如下所示:
function my_exec($cmd, $input=''){
$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes);
fwrite($pipes[0], $input);fclose($pipes[0]);
$stdout=stream_get_contents($pipes[1]);fclose($pipes[1]);
$stderr=stream_get_contents($pipes[2]);fclose($pipes[2]);
$rtn=proc_close($proc);
return array('stdout'=>$stdout,
'stderr'=>$stderr,
'return'=>$rtn
);
}
var_export(my_exec('echo asdf > asdf.txt'));
我从以下网址获得了这些脚本: http : //php.net/manual/en/function.system.php#94929
然后,您可以在浏览器“网络检查器”中检查结果。 但是在指导您之前,我觉得您的handleSubmit()
函数存在问题。 我相信它将无法正确地preventDefault()
。 因为我们使用的是React,默认情况下它将具有“合成事件”。 我相信,要正确地做到preventDefault()
,我们应该做到以下preventDefault()
:
handleSubmit(event){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
}.bind(this)
xmlHttp.open("GET","test.php",true)
xmlHttp.send(null)
event.nativeEvent.preventDefault();//This is how we really preventDefault() the native event, instead synthetic event
}
然后,单击“提交”后,您应该能够检查来自PHP脚本的响应。
我建议将my_exec()
用作要调试的辅助函数。 一旦感觉到它可以正常工作,请将其替换为system()
或exec()
或您喜欢的任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.