admin管理员组文章数量:1426810
I'm struggling with a python program and an ajax request. I'm trying to get some data from my Javascript into the python program, the normal method I've been using .getfirst(field name) doesn't work which I assume is because the request is via ajax (sorry, I'm quite new to all this) so I've tried using the following code
Python:
import MySQLdb
import cgi, cgitb
def index(req):
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
dtbox = form.getvalue('dt')
tmbox = form.getvalue('tm')
con = MySQLdb.connect('localhost', 'root', '', 'mydb')
with con:
cur = con.cursor(MySQLdb.cursors.DictCursor)
s = "SELECT tmp, watts FROM currentcost WHERE dt ='" + dtbox + "' and tm like '" + tmbox + "%'"
cur.execute (s)
rows = cur.fetchall()
x=""
y=""
for row in rows:
x=x+row["watts"]+","
y=y+row["tmp"]+","
x="data:["+x+"]"
y="data:["+y+"]"
con.close()
req.write(x)
Javascript snippet:
function draw(handleResponse) {
$.ajax({
url: "/currentcost.py",
data: {dt: frm.dt, tm: frm.tm},
success: function(response){
handleResponse(response);
}
});
<form name="frm" target="ifrm">
<iframe name="ifrm" id="ifrm" style="display:none"></iframe>
<fieldset style="width:300px">
<legend>Chart Date and Time</legend>
Alter the date and time settings <br>
Date:
<select name="dt">
I'm expecting the form values dt and tm to be transferred to the python program where it will pick them out and run through my select query ... all I get back is a blank :-(
Thanks in anticipation of your help
Chris
I'm struggling with a python program and an ajax request. I'm trying to get some data from my Javascript into the python program, the normal method I've been using .getfirst(field name) doesn't work which I assume is because the request is via ajax (sorry, I'm quite new to all this) so I've tried using the following code
Python:
import MySQLdb
import cgi, cgitb
def index(req):
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
dtbox = form.getvalue('dt')
tmbox = form.getvalue('tm')
con = MySQLdb.connect('localhost', 'root', '', 'mydb')
with con:
cur = con.cursor(MySQLdb.cursors.DictCursor)
s = "SELECT tmp, watts FROM currentcost WHERE dt ='" + dtbox + "' and tm like '" + tmbox + "%'"
cur.execute (s)
rows = cur.fetchall()
x=""
y=""
for row in rows:
x=x+row["watts"]+","
y=y+row["tmp"]+","
x="data:["+x+"]"
y="data:["+y+"]"
con.close()
req.write(x)
Javascript snippet:
function draw(handleResponse) {
$.ajax({
url: "/currentcost.py",
data: {dt: frm.dt, tm: frm.tm},
success: function(response){
handleResponse(response);
}
});
<form name="frm" target="ifrm">
<iframe name="ifrm" id="ifrm" style="display:none"></iframe>
<fieldset style="width:300px">
<legend>Chart Date and Time</legend>
Alter the date and time settings <br>
Date:
<select name="dt">
I'm expecting the form values dt and tm to be transferred to the python program where it will pick them out and run through my select query ... all I get back is a blank :-(
Thanks in anticipation of your help
Chris
Share Improve this question edited Sep 24, 2013 at 14:53 CHRIS LEONARD asked Sep 24, 2013 at 9:47 CHRIS LEONARDCHRIS LEONARD 791 gold badge1 silver badge8 bronze badges 6- I've fixed the formatting in your question. And please learn the difference between Java and Javascript. – Daniel Roseman Commented Sep 24, 2013 at 9:55
-
You don't seem to define
frm
in your JS. Is it defined somewhere else? You should show where. – Daniel Roseman Commented Sep 24, 2013 at 9:56 - 3 This isn't related to your Javascript question, but your current code is vulnerable to SQL injections. – user2629998 Commented Sep 24, 2013 at 10:01
- @Daniel Sorry, must have been rushing too much, I do understand difference between Java and Javascript. I've editted my code above and added the few lines which refer to the form definitions (I've not included the whole page as its huge). I think I've been doing this for so long now I can't see the wood for the trees!! Any help would be appreciated – CHRIS LEONARD Commented Sep 24, 2013 at 14:56
- @Andre Thanks, I've not even looked at SQL Injection issues yet and as its running on my Raspberry PI its not a huge priority, but I take your point, once working I need to look at these points. Thanks – CHRIS LEONARD Commented Sep 24, 2013 at 14:57
1 Answer
Reset to default 1Your ajax call should be of type "POST"
, and you can serialize the fields from the form using .serialize()
.
$.ajax({
url: "/currentcost.py",
type: "POST",
data: $("form[name='frm']").serialize(),
success: function(response){
handleResponse(response);
}
});
Edit
You should not typically use GET requests for form submission. That said, the ajax GET should look like:
$.ajax({
url: "/currentcost.py",
data: {dt: $("#dt").val(), tm: $("#tm").val() },
success: function(response){
handleResponse(response);
}
});
This assumes you have inserted the attribute id="dt"
into the first element and id="tm"
into the second element.
本文标签: javascriptHow to post form data via ajax to a python scriptStack Overflow
版权声明:本文标题:javascript - How to post form data via ajax to a python script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745474019a2659878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论