从python文件中的ajax发布请求获取发布数据 [英] Get post data from ajax post request in python file
问题描述
我试图用ajax发布请求发布一些数据并执行python文件,在python文件中检索数据,然后返回结果.
我有以下ajax代码
$(function () {
$("#upload").on("click", function (e) {
$.ajax({
type: 'post',
url: "test1.py",
data: {'param1':'abc'},
async: false,
success: function (response) {
console.log(response);
}
}).done(function (data) {
console.log(data);
});
});
});
以及以下python文件
#! /usr/bin/python
from __future__ import print_function
import cgi, cgitb
def index():
data = cgi.FieldStorage()
mydata = data['param1'].value
return return "test"
我在param1上遇到键盘错误->"KeyError:'param1'".我也尝试过使用getValue(data ['param1'].将数据从ajax调用传输到我要执行的python文件中似乎有问题...但是我不知道为什么./p>
预先感谢
我设法提出了一个解决方案.我希望其他人也可以使用它.
通过以下python代码,我设法从ajax调用中获取postdata.
def index(req):
postData = req.form
json = str(postData['param'].value)
I'm trying to post some data with an ajax post request and execute a python file, retrieving the data in the python file, and return a result.
I have the following ajax code
$(function () {
$("#upload").on("click", function (e) {
$.ajax({
type: 'post',
url: "test1.py",
data: {'param1':'abc'},
async: false,
success: function (response) {
console.log(response);
}
}).done(function (data) {
console.log(data);
});
});
});
And the following python file
#! /usr/bin/python
from __future__ import print_function
import cgi, cgitb
def index():
data = cgi.FieldStorage()
mydata = data['param1'].value
return return "test"
I'm getting a keyerror on param1 -> "KeyError: 'param1'". I've also tried to use getValue(data['param1']. It looks like a problem with transferring the data from the ajax call to the python file i want to execute... But i can't figure out why.
Thanks in advance
I've managed to come up with an solution. I hope other people can get use of it as well.
By the following python code i've manged to get the postdata from the ajax call.
def index(req):
postData = req.form
json = str(postData['param'].value)
这篇关于从python文件中的ajax发布请求获取发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!