是什么导致 jQuery Ajax 调用 Python 脚本抛出 500 错误?

问题描述 投票:0回答:1

我有一个网站,其中包含对 Python 脚本的 jQuery Ajax 调用。当我调用它时,它返回 500 错误。请注意,Python 脚本本身在通过 cron 作业提交时可以工作;只有当尝试通过 Ajax 调用它时才会出现错误。

进行调用的 HTML 页面摘录:

<!DOCTYPE html>
<html>
<head>
<title>Python Test</title>
<script src="jquery-3.3.1.js"></script>
</head>
<body>
<div id="the_output">The text returned from the Python script goes here</div>

<script>

$.ajax({
         url:"cgi-bin/PythonTest.py",
         type:"POST",
         success:function(data) {
             try {
                 document.getElementById("the_output").innerHTML = data;
             }
             catch (e) {
                 document.getElementById("the_output").innerHTML = "<b>Error parsing data:</b><br />" + data;
             }
         },
         error:function(xhr, status, error) {
            let errorMessage = "<b>Query returned a failure:</b><br />"
                             + "Status: " + status + "<br />"
                             + "Error: " + error + "<br />"
                             + "XHR Response Text:<br />{" + xhr.responseText + "}";
            document.getElementById("the_output").innerHTML = errorMessage;
         }
       });
</script>

</body>
</html>

和PythonTest.py:

#!/usr/bin/env python3

print("<br />If you can read this, then It Works<br />")

当我在浏览器中输入页面的 URL 时,返回的是:

查询返回失败

状态:错误

错误:内部服务器错误

XHR 响应包含 500 错误的 Web 主机页面的 HTML。

我应该在 Ajax 调用中添加一些内容吗?

python jquery ajax
1个回答
0
投票

如果没有看到 python 脚本,很难判断,但有几件事需要检查:

  1. 确保脚本顶部有
    #!/usr/bin/python
    。大多数 cgi 环境都需要这个。
  2. 确保您的 Web 服务器配置允许 CGI 执行。您没有指定服务器,但对于 apache 服务器,这意味着确保启用 ExecCGI 选项。
© www.soinside.com 2019 - 2024. All rights reserved.