0

I use flask 0.12 and try to send a PUT request using ajax. However, I get an error No JSON object could be decoded when use .get_json() method. The content is json and is_json returns True. There is my client-side code:

$.ajax({
    url: apiTechnicians,
    type: 'PUT',
    data: { "technicianUserId": chosenTechnician.val() },
    contentType: "application/json",
    dataType: "json",
    success: function (result) {
        console.log(result)
    }
});

And there is the server-side code:

@api.route("/technicians", methods=['PUT'])
def putTechnicians():
    print request.is_json
    jsonResult = request.get_json()
    return Response(status=200)

Any suggestions how should I work with the received data and why get_json() can't decode the data?

Vitalii Isaenko
  • 941
  • 1
  • 14
  • 37

1 Answers1

1

You aren't sending JSON, you're saying its json(via content type) but its not.
jQuery.ajax converts an object passed to it to application/x-www-form-urlencoded data, so to send json data you have to pass json data to jQuery.ajax

$.ajax({
    url: apiTechnicians,
    type: 'PUT',
    data: JSON.stringify({ "technicianUserId": chosenTechnician.val() }),
    contentType: "application/json",
    success: function (result) {
        console.log(result)
    }
});
Musa
  • 96,336
  • 17
  • 118
  • 137