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);
      }
davidism
  • 121,510
  • 29
  • 395
  • 339
min2bro
  • 4,509
  • 5
  • 29
  • 55

1 Answers1

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.

mata
  • 67,110
  • 10
  • 163
  • 162
  • I'm getting a None Type for the search variable, Not sure why cmpname value is not fetched. – min2bro Dec 20 '17 at 14:41
  • 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. – mata Dec 20 '17 at 15:25