I have trouble getting the data from an ajax request.I can access the data through Djangos' request.GET (and other POST, DELETE etc. headers), but not with the REST request.data (or request.body) which returns an empty dict. My ajax call:
function getMeal(event)
{
var tmp = event._id.split("_")
var database = tmp[0]
var mealId = tmp[1]
$.ajax(
{
type: "GET",
url: "{% url 'updateEatenMealAjax' %}",
data:
{
'database': database,
'mealId' : mealId,
},
success: function(data, textStatus, jqXHR)
{
$('#update_EatenMeal_FormBody').html(data);
},
dataType : 'html',
async: 'false',
contentType: 'application/json'
});
}
My django view:
@login_required
@api_view(["PUT", "GET", "DELETE"])
@csrf_protect
@ensure_csrf_cookie
def updateEatenMealAjax(request):
args = {}
eaten_object = None
# WHICH DATABASE DOES THIS FOOD ITEM BELONG TO
database = request.data.get('database')
mealId = request.data.get('mealId')