Get post data from ajax post request in python file

2024/10/4 11:16:26

I'm trying to post some data with an ajax post request and execute a python file, retrieving the data in the python file, and return a result.

I have the following ajax code

        $(function () {$("#upload").on("click", function (e) {$.ajax({type: 'post',url: "test1.py",data: {'param1':'abc'},async: false,success: function (response) {console.log(response);}}).done(function (data) {console.log(data);});});});

And the following python file

    #! /usr/bin/pythonfrom __future__ import print_functionimport cgi, cgitbdef index():data = cgi.FieldStorage()mydata = data['param1'].valuereturn return "test"

I'm getting a keyerror on param1 -> "KeyError: 'param1'". I've also tried to use getValue(data['param1']. It looks like a problem with transferring the data from the ajax call to the python file i want to execute... But i can't figure out why.

Thanks in advance

Answer

I've managed to come up with an solution. I hope other people can get use of it as well.

By the following python code i've manged to get the postdata from the ajax call.

    def index(req):postData = req.formjson = str(postData['param'].value)
https://en.xdnf.cn/q/70610.html

Related Q&A

How to implement maclaurin series in keras?

I am trying to implement expandable CNN by using maclaurin series. The basic idea is the first input node can be decomposed into multiple nodes with different orders and coefficients. Decomposing singl…

Rowwise min() and max() fails for column with NaNs

I am trying to take the rowwise max (and min) of two columns containing datesfrom datetime import date import pandas as pd import numpy as np df = pd.DataFrame({date_a : [date(2015, 1, 1), date(2012…

Convert column suffixes from pandas join into a MultiIndex

I have two pandas DataFrames with (not necessarily) identical index and column names. >>> df_L = pd.DataFrame({X: [1, 3], Y: [5, 7]})>>> df_R = pd.DataFrame({X: [2, 4], Y: [6, 8]})I c…

sys-package-mgr*: cant create package cache dir when run python script with Jython

I want to run Python script with Jython. the result show correctly, but at the same time there is an warning message, "sys-package-mgr*: cant create package cache dir"How could I solve this p…

Python WWW macro

i need something like iMacros for Python. It would be great to have something like that:browse_to(www.google.com) type_in_input(search, query) click_button(search) list = get_all(<p>)Do you know …

Django custom context_processors in render_to_string method

Im building a function to send email and I need to use a context_processor variable inside the HTML template of the email, but this dont work.Example:def send_email(plain_body_template_name, html_body_…

Using string as variable name

Is there any way for me to use a string to call a method of a class? Heres an example that will hopefully explain better (using the way I think it should be):class helloworld():def world(self):print &…

How to sum all amounts by date in pandas dataframe?

I have dataframe with fields last_payout and amount. I need to sum all amount for each month and plot the output. df[[last_payout,amount]].dtypeslast_payout datetime64[ns] amount float64 d…

Unable to import decimal in Python 2.7 or Python 3.3 [duplicate]

This question already has answers here:Importing a library from (or near) a script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError(4 answers…

I Get ImportError: No module named pathlib, even after installing pathlib with pip

This is my first time asking on this site, so sorry if my question is not layed out correctlyy@DESKTOP-MQJ3NCT:~/Real-Time-Voice-Cloning$ python demo_toolbox.py Traceback (most recent call last):File &…