Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My Python code-:
Python
from flask import Flask
from flask_cors import CORS
import cgi

app = Flask(__name__)
CORS(app)

form = cgi.FieldStorage()

@app.route("/", methods=['POST'])
def apicall():
  var1 = form.getvalue('var1')
  if len(var1)>1:
    return 'received value from JS function'   

if __name__ == "__main__":
  app.run()


My JS function -:
JavaScript
function callPythonScript() {
    alert("called_python_script");
    $.ajax({
        url: "http://127.0.0.1:5000",
        type: "POST",
        data:{
            var1: window.location.href,
        },
        success: function(response) {
            console.log(response);
        },
        error: function(xhr) {
            console.log(xhr);
        }


python throws this error - TypeError: object of type 'NoneType' has no len()
127.0.0.1 - - [31/Oct/2022 00:28:01] "POST / HTTP/1.1" 500 -

is there any configuration i need to do to my flask server in order to accept values from js

What I have tried:

import cgi
form = cgi.FieldStorage()
var1 = form.getvalue('var1')
Posted
Comments
Richard Deeming 31-Oct-22 5:49am    
Use your browser's developer tools to debug the AJAX request to check how the body is encoded.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900