0

Python code -

from flask import Flask
from flask_cors import CORS
import cgi

app = Flask(__name__)
CORS(app)

form = cgi.FieldStorage()
var1 = form.getvalue('var1')

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

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

error-: TypeError: object of type 'NoneType' has no len().- "POST / HTTP/1.1" 500 -

My javascript function -

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);
                    }

do i need configure my flask server to accept values from js func, what i have tried is -

import cgi 
form = cgi.FieldStorage()
var1 = form.getvalue('var1')

PS: this setup works perfectly if i just pass return "Hello world" in python funx apicall()

  • You need to move ```var1 = form.getvalue('var1')``` inside the body of ```apicall()``` and you have to only use that code when you have a ```POST``` method – NoCommandLine Oct 30 '22 at 18:35
  • @NoCommandLine , i have tried that way previously, moving var1 line inside my function call, it still doesnt work and returns ** TypeError: object of type 'NoneType' has no len() 127.0.0.1 - - [31/Oct/2022 00:12:53] "POST / HTTP/1.1" 500 - ** – nooblearner Oct 30 '22 at 18:43
  • 1). It should be inside that function. Its current location means it's always called immediately your file loads and the value will be None. 2) You're passing/trying to retrieve data the wrong way. See - https://stackoverflow.com/a/37631988/15211203 – NoCommandLine Oct 30 '22 at 18:56
  • @NoCommandLine , followed both your suggestions and made modifications to my code accordingly , still no luck `@app.route("/", methods=['GET','POST']) def apicall(): var1 = None if request.method == "POST": var1 = form.getvalue('var1') if len(var1)>1: return 'received value from JS function' ` – nooblearner Oct 30 '22 at 20:05
  • @Fastnlight , could you please look at this bug. – nooblearner Oct 30 '22 at 20:15

1 Answers1

1

If you follow the example here, then your python code should be something like

@app.route("/", methods=['GET', 'POST'])
def apicall():
    if request.method == 'POST':
        data = request.get_json() # Get all the data passed in (as JSON)
        var1 = data.get('var1', None)
        if var1 and len(var1)>1:
            return 'received value from JS function' 
        else:
            return 'No value from JS function'
    else:
       # Code to handle GET 

Note that you're no longer using form.get.... because you passed in JSON and not form data

Even if you submit a form, it's usually better to acccess the values as

request.values.get(<var_name>, <default_value>)

The above method allows you to specify a default value in the event <var_name> isn't available

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15
  • Thanks for your help , apart from this i just had to add `contentType: 'application/json;charset=UTF-8',` into my ajax function and it worked. – nooblearner Oct 31 '22 at 03:52