通过 jQuery AJAX POST 数据到 Python CGI 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20723952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST data to Python CGI script via jQuery AJAX
提问by adeora
I'm trying to setup a simple script where some data is sent using the jQuery .ajax function to a Python CGI script. The Python script would just make the data posted to it uppercase, and then return that data to the HTML file, where a div would be updated with the content.
我正在尝试设置一个简单的脚本,其中使用 jQuery .ajax 函数将一些数据发送到 Python CGI 脚本。Python 脚本只会将发布到它的数据设为大写,然后将该数据返回到 HTML 文件,其中的 div 将使用内容更新。
I have the code shown below. When I run it, the AJAX call executes, but the div is not updated with the content.the div is not updated with the data being sent.
我有下面显示的代码。当我运行它时,AJAX 调用会执行,但div 没有更新内容。div 不会随着发送的数据而更新。
How would I modify this code so that it updates with the data being sent?
我将如何修改此代码,以便它随着发送的数据进行更新?
I appreciate any help at all.
我很感激任何帮助。
My HTML Code:
我的 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Python-jQuery Example</title>
<script src="http://code.jquery.com/jquery-2.0.3.js"></script>
<script>
$(function()
{
$.ajax({
url: "http://localhost/cgi-bin/post.py",
type: "post",
datatype: "html",
data: "here is data",
success: function(response){
$("#div").html(response);
console.log("There is a response");
}
});
});
</script>
</head>
<body>
<div id="div">Default Stuff</div>
</body>
My Python Code:
我的 Python 代码:
#!/usr/bin/python
import cgi, cgitb
cgitb.enable()
data = cgi.FieldStorage()
print "Content-Type: text/html"
print data
EDIT: I have updated my code to what is currently shown, and now the document updates with this string:
编辑:我已将代码更新为当前显示的内容,现在文档更新为以下字符串:
FieldStorage(None, None, [])
How would I modify my code so that the div updates with the data being sent?
我将如何修改我的代码,以便 div 随发送的数据更新?
采纳答案by Ricky Levi
I believe that the "data" inside the $.ajax should be like a hash of a key-value pairs. try:
我相信 $.ajax 中的“数据”应该像一个键值对的散列。尝试:
$.ajax(
....
data: {
a: "aa",
b: "bb"
}
...
If you see in the div something like:
如果您在 div 中看到类似:
MiniFieldStorage('a', 'aa')
Then you should access it via:
然后你应该通过以下方式访问它:
my_param = cgi.getfirst('a')
print "Content-Type: text/html\n"
print my_param
If it's still not working, can you test it via GET instead of POST ? In addition, what's the structure of the AJAX file(s) that you're accessing ? ( is the file inside a sub-directory ? etc... )
如果它仍然无法正常工作,您可以通过 GET 而不是 POST 来测试它吗?此外,您正在访问的 AJAX 文件的结构是什么?(是子目录中的文件吗?等等...)
回答by Sabuj Hassan
I think you need to use a newline with the content type printing.
print "Content-Type: text/html\n"
Also, you need to provide the parameter name with your variable data. Assuming you are posting the value "xyz" with the parameter "param". So it should be: data["param"]at where the xyz resides.
print data["xyz"]
If the error still persist, then browse the url localhost/cgi-bin/post.py from the browser and check what does it return(with a GET parameter for testing of course).
我认为您需要在内容类型打印中使用换行符。
打印“内容类型:文本/html\n”
此外,您需要提供参数名称和变量data。假设您使用参数“param”发布值“xyz”。所以它应该是:data["param"]在 xyz 所在的位置。
打印数据["xyz"]
如果错误仍然存在,则从浏览器浏览 url localhost/cgi-bin/post.py 并检查它返回什么(当然带有 GET 参数用于测试)。
回答by adeora
I figured out that the data needs to be formatted as key-value pairs, like so:
我发现数据需要格式化为键值对,如下所示:
$.ajax({
url: "http://localhost/cgi-bin/variousTests/post.py",
type: "POST",
data: {foo: 'bar', bar: 'foo'},
success: function(response){
$("#div").html(response);
}
});
In the Python script, I used this code to get the data from the POST request (it works the exact same way if a GET request is used):
在 Python 脚本中,我使用此代码从 POST 请求中获取数据(如果使用 GET 请求,它的工作方式完全相同):
#!/usr/bin/python
import cgi, cgitb
cgitb.enable() # for troubleshooting
#the cgi library gets vars from html
data = cgi.FieldStorage()
#this is the actual output
print "Content-Type: text/html\n"
print "The foo data is: " + data["foo"].value
print "<br />"
print "The bar data is: " + data["bar"].value
print "<br />"
print data

