大家好,我在使用 PHP 或 AJAX 通过我的网站调用 python 脚本时遇到问题
我所有的东西,html、php、css 和 .py 都在同一个文件夹/var/www/html/
JS:
document.getElementById('print').onclick = function(){Print()};
function Print() {
var param = 'zya';
$.ajax({
type: 'POST',
url: '../degaps1.py',
data: {param: param},
success: function(response){
output = response;
alert(output);
}
});
Python:
degaps1.py
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html"
print ""
arguments = cgi.FieldStorage()
print "test"
当我使用不带“../”的“url”时,它会打印整个代码,现在我在控制台中收到一条消息... 403 Forbidden。我能做什么来修复它?提前致谢!
如果我没记错的话,警报中的输出应该是“test”。
最佳答案
你不能像这样随时随地调用脚本,你最多只能得到文件的内容。您需要一台服务器来执行...您知道...服务器端的工作,例如处理请求。
看看Flask 。它是一个小型的 python 服务器,不需要花时间来设置它,一旦设置好,你就可以通过 Javascript 调用你的脚本。你也可以寻找其他解决方案,但我记得当我需要使用 Python 而不是 PHP 时,Flask 是我的救星!
关于javascript - PHP/AJAX 调用 Python - 403 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289570/