进行AJAX调用,将下拉值传递给python脚本 [英] Make an AJAX call to pass drop down value to the python script

查看:145
本文介绍了进行AJAX调用,将下拉值传递给python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下拉列表中选择包含数据库名称的值,并将其传递给连接到传递的数据库名称的后台的python脚本。
以下是我写的ajax代码

 < script type =text / javascript> 
$(document).ready(function(){
$(button)。click(function(){
$ .ajax({$ b $ url:/ form_submit ,
data:$('#databases')。val(),
type:'POST',
success:alert(Hi dear count+ $('#databases' ).val())
});
});
});
< / script>

databases是HTML中select标签的id。我在写数据:

  $('#databases')。val()

将数据传递给Python代码。



以下是python代码,它应该接受传递的值。如果我直接从控制台运行下面的代码,那么它以json格式返回结果,但间接运行它并没有成功。

  @ ():
import json
dtb = request.select ['value' ]
db = MySQLdb.connect(localhost,root,,dtb)
cursor = db.cursor()
cursor.execute(SELECT * FROM REPORT_SUITE)
results = cursor.fetchall()
json_return_value = []

表示结果的结果:
table_data = {'REPORTSUITE_ID':result [0],'REPORTSUITE_NAME ':result [1],'STAGING_DATABASE':result [2],'DWH_DATABASE':result [3],'TRANS_TABLE':result [4]}
json_return_value.append(table_data)
print( hi)
print json.dumps(json_return_value)
return json.dumps(json_return_value)

我已将变量声明为 dtb = request .select ['value'] 它应该接受通过AJAX调用传递的数据库名称。
另外我应该能够在我的Web浏览器中看到JSON格式的返回数据。
我环顾四周,应用了许多建议的解决方案,但我仍然无法确定如何传递并捕获传递的值。 解决方案

对于 POST 请求,传递的值可以是获得

  request.form ['value'] 


I want to pass the selected value from dropdown which contains names of databases and pass it to the python script in the background which connects to the passed database name. Following is the ajax code that i have written

<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $.ajax({
          url : "/form_submit",
          data : $('#databases').val(),
          type : 'POST',
          success : alert("Hi dear count " + $('#databases').val())
        });
        });
    });
</script>

The "databases" is the id of the select tag in HTML. I am writing data :

$('#databases').val() 

to pass the data to the python code.

Following is the python code which should accept the passed value. If i run the below code directly from console, then it returns the result in json format but running it indirectly has not succeeded

@app.route("/form_submit/", methods=['GET','POST'])
def connect():
    import json
    dtb = request.select['value']
    db = MySQLdb.connect("localhost","root","",dtb)
    cursor =  db.cursor()
    cursor.execute("SELECT * FROM REPORT_SUITE")
    results = cursor.fetchall()   
    json_return_value =[]

    for result in results:
        table_data = {'REPORTSUITE_ID' : result[0], 'REPORTSUITE_NAME' : result[1], 'STAGING_DATABASE' : result[2], 'DWH_DATABASE' : result[3], 'TRANS_TABLE' : result[4]}
        json_return_value.append(table_data)
    print ("hi")
    print json.dumps(json_return_value)
    return json.dumps(json_return_value)

I have declared the variable as dtb = request.select['value'] which should accept the database name passed through AJAX call. Also i should be able to see the returned data in JSON format in my web browser. I have looked around and applied many suggested solutions but i still am unable to determine how to pass and catch the passed value.

解决方案

For POST requests, the passed value can be obtained by

request.form['value']

这篇关于进行AJAX调用,将下拉值传递给python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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