0

I am developing a simple web app based on the Python framework Flask. Here I am trying to get a JSON response from a POST AJAX request. Below code shows my AJAX call:

var hotel=$( "#listHotel option:selected" ).val();
$.ajax({
    url: "/getHotels",
    contentType: "application/xml; charset=utf-8",
    data: JSON.stringify({'hotel':hotel}),
    type: "POST",
    success: function(response){
        var r= JSON.parse(response);
        var rating =r.message
        alert(rating);
        $("#rate").html("Ratings : "+rating);
        $("#rate").show('slow');
        console.log(response);
    },
    error: function(error){
        alert(response);
        console.log(error);
    }
});

And this is my Python code

def getHotels():
    try:
        _hotel = {"value": request.json['hotel']}
        print _hotel
        hotel= _hotel["value"]
        print hotel

But I am not getting the JSON object. I tried my best. Please help me to get the JSON object / value.

poke
  • 369,085
  • 72
  • 557
  • 602
Prem
  • 630
  • 2
  • 11
  • 25

1 Answers1

0

since you are just passing a single value in your ajax request, you can replace the line

data: JSON.stringify({'hotel':hotel}),

by

data: {'hotel': hotel},

From there, you can access the value in flask by doing

request.form['hotel']
tagette
  • 68
  • 4