0

My Javascript post url is as shown below with %20 in between i.e. spaces, when I'm trying to access this URL with Flask the search variable is shown as type None.

Am I parsing the url parameter cmpname correctly inside showMapMArker() function, Not sure about it.

POST /getLatLong/Ogden%20UT%20844

Python:

@app.route('/getLatLong/<cmpname>',methods=['GET','POST'])
def showMapMarker(cmpname):
    # print("Inside ShowMarker")
    search = request.args.get(cmpname)

//do something with search string

return jsonify(search)

Javascript:

function myFunction() {
              var cmpCanName = document.getElementById("autocomplete").value;
              var cmpCanAddress = document.getElementById("autocompletetwo").value;

          var xhttp = new XMLHttpRequest();
          xhttp.open("POST", "/getLatLong/"+cmpCanAddress, true);
          xhttp.setRequestHeader("Content-type", "application/json");
          xhttp.send();
          var response = JSON.parse(xhttp.responseText);
      }
0

1 Answer 1

1

showMapMarker(cmpname) will be called with cmpname set to the value given in the route '/getLatLong/<cmpname>'. You then use that value for a lookup in the request arguments, which will result in None if the request has no such argument. So to not return None, the request URL would have to look like this:

http://example.com/getLatLong/ham?ham=spam

Now search would be the string spam, ham can be freely chosen by the caller. I don't think that is what you intended, you probably just want to use the cmpname argument as passed to the function.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting a None Type for the search variable, Not sure why cmpname value is not fetched.
You don't need to, it's passed to the function by the framework. Just try search = cmpname, that should be it. Your request doesn't seem to have either arguments or post data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.