Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a jQuery Request to Django REST Framework Results in no found JSON object error

I'm using Django's REST Framework to power an API, and sending an Ajax request to it using jQuery.

The following cURL works fine:

curl -X POST -d '{"timeLogMins": 30, "personid": 3, "projectid": 8, "timeLogStart_dtm": "2013-07-18"}' -H "Content-Type: application/json" -u user:password http://localhost:8000/api/timelogests/

however, when I do a an ajax call using jquery, I'm getting an error

$.ajax({
    url: update_url2,
    contentType:"application/json",
    headers: {
        "Authorization": "Basic " + window.btoa("user:password"),
    },
    dataType: "json",
    data: {
            "timeLogMins":30,
            "personid":personid,
            "projectid":projectid,
            "timeLogStart_dtm":start_date
            },
    type: 'POST',

}).error(function(r){ console.log(r) })
.success(function(r){ console.log("success", r) })

Setting the header works fine, but the data seems to return:

"{"detail": "JSON parse error - No JSON object could be decoded"}"

I started working with REST Framework yesterday, so still quite new to it. Is there a way I can intercept the request header and inspect that JSON? The REST Framework request object isn't very visible.

like image 625
NotSimon Avatar asked Jul 18 '13 23:07

NotSimon


1 Answers

It turns out that turning the data sent into an actual string will make the JSON readable by the framework. According to the jQuery documentation this should happen either way, but it seems to have broken this specific data object lay out? This solves my problem, but I'm not sure what's wrong.

like image 86
NotSimon Avatar answered Oct 11 '22 12:10

NotSimon