ajax传值给python,进行AJAX调用,将下拉值传递给python脚本

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

$(document).ready(function(){

$("button").click(function(){

$.ajax({

url : "/form_submit",

data : $('#databases').val(),

type : 'POST',

success : alert("Hi dear count " + $('#databases').val())

});

});

});

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']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值