网页js调用php执行Python传参不能有中文,有中文就报错我们的Jj代码大概是这样的
resyltstr="@_主食_@_@_55";
xmlhttp.open("POST","..........q="+resultstr,True)
php代码
$q=["q"];
exce("Python /var/www/html/SERVER/DATABASE/main.py $q",$out);
Python代码
a=sys.argv[1]
with open("o.txt","w") as f:
print(a,file=f)这些代码在终端调用php可以将a写入文件通过网页执行Jj调用php时resultstr没有中文可以有中文就不行结果是Null
resyltstr="@_主食_@_@_55";
xmlhttp.open("POST","..........q="+resultstr,True)
php代码
$q=["q"];
exce("Python /var/www/html/SERVER/DATABASE/main.py $q",$out);
Python代码
a=sys.argv[1]
with open("o.txt","w") as f:
print(a,file=f)这些代码在终端调用php可以将a写入文件通过网页执行Jj调用php时resultstr没有中文可以有中文就不行结果是Null
<html>
<meta charset="UTF-8">
<title>website</title>
<body >
<center>helloword!</center>
<div id="txt" style="height: 50px;"></div>
<script>
var resultstr="@_主食_@_@_55";
var xmlHttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(!xmlhttp){
alert("创建对象失败!");
}
else{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("POST","index2.php",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("q="+resultstr);
}
function state_Change(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
if(xmlhttp.responseText!=""){
alert(xmlhttp.responseText);
}else{
alert("error in js go php!");
}
}
}
</script>
</body>
</html>
<?php
header('Content-type: text/html;charset=GB2312');
$q=$_POST["q"];
$c=exec("Python f:\\vscode\\python\\work.py ".$q,$out,$res);
if ($res==0){
echo $c;
}else{
echo "error php go python!";
}
?>#coding:utf-8
import sys
a=sys.argv[1]
with open("o.txt","w") as f1:
f1.write(a)
print(a)