Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用AJAX运行Python服务器代码?_Python_Ajax - Fatal编程技术网

如何使用AJAX运行Python服务器代码?

如何使用AJAX运行Python服务器代码?,python,ajax,Python,Ajax,我试图从网页发送一个简单的jquery ajax XMLHttpRequest,以便在服务器上运行python程序(无需使用Flask或Django之类的框架)。我在这里和Quora上看过很多例子,但都没有找到任何我可以使用的东西。我可以让ajax在页面中插入文本文件中的文本,或者运行php代码,就像所有的书都会介绍的那样,我也可以用老式的cgi方式从表单中运行python代码,但是这里似乎有什么东西阻止了python的ajax成功函数的启动。 希望我没有从根本上误解异步的工作原理,但这似乎很常

我试图从网页发送一个简单的jquery ajax XMLHttpRequest,以便在服务器上运行python程序(无需使用Flask或Django之类的框架)。我在这里和Quora上看过很多例子,但都没有找到任何我可以使用的东西。我可以让ajax在页面中插入文本文件中的文本,或者运行php代码,就像所有的书都会介绍的那样,我也可以用老式的cgi方式从表单中运行python代码,但是这里似乎有什么东西阻止了python的ajax成功函数的启动。 希望我没有从根本上误解异步的工作原理,但这似乎很常见。 我在控制台里看到的只是

jquery-3.5.1.js:10099 POST example.com/cgi-bin/test3.cgi 500 (Internal Server Error)
send @ jquery-3.5.1.js:10099
ajax @ jquery-3.5.1.js:9682
ProcessSimpleCgi @ quora_index.html:9
onclick @ quora_index.html:30
如果有人能指出我哪里出了问题,我将不胜感激

HTML: 
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.5.1.js">
</script>
<script>
function ProcessSimpleCgi()
{
  $.ajax(
{ 
    type: "POST",
    url: "/cgi-bin/test3.cgi", 
    dataType: "html",
    success: function(data) { 
            $("#output").html(data);
    }, 
    error: function() {
        $("#output").html("nothing happened");
    }
  }); 
}
</script>
</head>
<body>
ajax python attempt 3<p>
<input type="button" value="go" onclick="ProcessSimpleCgi()"><p>
<div id = "output"></div>
</body>
</html>

test3.cgi:
#!/usr/bin/python
import cgi, cgitb
cgitb.enable()
print "Content-Type: text/html"
print
print "<html><body><h2>Hello World!</h2></body></html>" 
HTML:
函数ProcessSimpleCgi()
{
$.ajax(
{ 
类型:“POST”,
url:“/cgi-bin/test3.cgi”,
数据类型:“html”,
成功:函数(数据){
$(“#输出”).html(数据);
}, 
错误:函数(){
$(“#输出”).html(“什么都没发生”);
}
}); 
}
ajax python尝试3

test3.cgi:
#!/usr/bin/python
导入cgi,cgitb
cgib.enable()
打印“内容类型:文本/html”
打印
打印“你好,世界!”