如何通过ajax将表单数据发布到python脚本? [英] How to post form data via ajax to a python script?

查看:100
本文介绍了如何通过ajax将表单数据发布到python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力处理python程序和ajax请求。我试图从我的Javascript中获取一些数据到python程序中,我一直在使用的常规方法.getfirst(字段名称)不起作用,我认为这是因为请求是通过ajax(对不起,我是所有这些都是新的)所以我尝试使用以下代码

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片段:

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">

我期待将表单值dt和tm传输到python程序,它将选择他们出来并通过我的选择查询...我得到的所有内容都是空白: - (

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 :-(

感谢您的帮助

Chris

推荐答案

您的ajax调用类型应为POST,您可以使用 .serialize()序列化表单中的字段。

Your 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);
    }
});

编辑

您通常不应该使用GET请求表单提交。也就是说,ajax GET应该如下所示:

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);
    }
 });

这假设您已插入属性 id =dt进入第一个元素, id =tm进入第二个元素。

This assumes you have inserted the attribute id="dt" into the first element and id="tm" into the second element.

这篇关于如何通过ajax将表单数据发布到python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆