Make an AJAX call to pass drop down value to the python script

2024/11/15 11:16:46

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 jsondtb = 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.

Answer

For POST requests, the passed value can be obtained by

request.form['value']
https://en.xdnf.cn/q/71787.html

Related Q&A

PyLint 1.0.0 with PyDev + Eclipse: include-ids option no longer allowed, breaks Eclipse integration

As noted in this question: How do I get Pylint message IDs to show up after pylint-1.0.0?pylint 1.0.0 no longer accepts "include-ids" option. (It returns "lint.py: error: no such optio…

Shifting all rows in dask dataframe

In Pandas, there is a method DataFrame.shift(n) which shifts the contents of an array by n rows, relative to the index, similarly to np.roll(a, n). I cant seem to find a way to get a similar behaviour …

Pandas dataframe: omit weekends and days near holidays

I have a Pandas dataframe with a DataTimeIndex and some other columns, similar to this:import pandas as pd import numpy as nprange = pd.date_range(2017-12-01, 2018-01-05, freq=6H) df = pd.DataFrame(ind…

How to dump a boolean matrix in numpy?

I have a graph represented as a numpy boolean array (G.adj.dtype == bool). This is homework in writing my own graph library, so I cant use networkx. I want to dump it to a file so that I can fiddle wit…

Cant append_entry FieldList in Flask-wtf more than once

I have a form with flask-wtf for uploading images, also file field can be multiple fields. my form: class ComposeForm(Form):attachment = FieldList(FileField(_(file)), _(attachment))add_upload = SubmitF…

What is the best way to use python code from Scala (or Java)? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Java Python Integration There is some code written in Python and I need to use it from Scala. The code uses some native C.…

Pandas groupby week given a datetime column

Lets say I have the following data sample:df = pd.DataFrame({date:[2011-01-01,2011-01-02,2011-01-03,2011-01-04,2011-01-05,2011-01-06,2011-01-07,2011-01-08,2011-01-09,2011-12-30,2011-12-31],revenue:[5,3…

Django form to indicate input type

Another basic question Im afraid which Im struggling with. Ive been through the various Django documentation pages and also search this site. The only thing I have found on here was back in 2013 which…

run multi command in the same jupyter cells

Im trying to display 2 output of 2 lines in the same time, I use Panda library and it seems like it display only the output of second line:import pandas as pd data = {"state": ["Ohio&quo…

Pandas how to get rows with consecutive dates and sales more than 1000?

I have a data frame called df: Date Sales 01/01/2020 812 02/01/2020 981 03/01/2020 923 04/01/2020 1033 05/01/2020 988 ... ...How can I get the first occurrence of 7 conse…