我是靠谱客的博主 靓丽楼房,最近开发中收集的这篇文章主要介绍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']

最后

以上就是靓丽楼房为你收集整理的ajax传值给python,进行AJAX调用,将下拉值传递给python脚本的全部内容,希望文章能够帮你解决ajax传值给python,进行AJAX调用,将下拉值传递给python脚本所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部