Aman Aggarwal Aman Aggarwal - 4 months ago 24
Ajax Question

Proper JSON not received in Django Request

I have a simple API where I get the JSON data from request and parse it. The above API works fine POSTMAN but does not work with jquery.

In views.py :

def search_and_return(request):
print(request.body.decode() , type(request))
request = request.body.decode()
request = json.loads(request)
client_id=int(request["order_clientId"])
start_date=request["order_start_dt"]
end_date=request["order_end_dt"]
print(client_id, start_date, end_date)


form.html :

<!DOCTYPE html>

<html>
<head>
<link rel="stylesheet" href="form.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript" src="form.js"></script>
</head>
<body>
<p>Client ID</p><input type="text" name="client_id" id="clientid"><br>
<p>Sub Client ID</p><input type="text" name="sub_client_id" placeholder="optional" id="subclientid"><br>
<p>Start Date(And Time)</p><input type="datetime-local" name="start_date" id="startdate"><br>
<p>End Date(And Time)</p><input type="datetime-local" name="end_date" id="enddate"><br>
<input id="button" type="submit" name="Submit">
</body>
</html>


In my javascript :

$(document).ready(function() {
$("#button").click(function() {
$.ajax({
url:"http://10.124.92.208:8000/customer/retrieve_info",
crossDomain:true,
data: {"order_clientId" : 114, "order_start_dt" : "2016-01-01T21:25:22", "order_end_dt" : "2016-01-05T21:25:22"},
type:"POST",
dataType:"json",
})
.done(function(json){
alert(json);
});
});

});


When I send request from POSTMAN I get below output as expected :

{"order_clientId" : 114, "order_start_dt" : "2016-01-01T21:25:22", "order_end_dt" : "2016-01-05T21:25:22"} <class 'django.core.handlers.wsgi.WSGIRequest'>

114 2016-01-01T21:25:22 2016-01-05T21:25:22


When I send request from custom html or JS, I get below output and error:

order_clientId=114&order_start_dt=2016-01-01T21%3A25%3A22&order_end_dt=2016-01-05T21%3A25%3A22 <class 'django.core.handlers.wsgi.WSGIRequest'>

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


I want to receive JSON data exactly same as what I receive from POSTMAN. What changes should I make ? I think this is a issue in Javascript because POSTMAN works perfectly. What is the best way to get the data from POST request in django-views?

Answer

You need to use JSON.stringify(arr) while parsing data

$(document).ready(function() {       
    $("#button").click(function() {
          $.ajax({
            url:"http://10.124.92.208:8000/customer/retrieve_info",
            crossDomain:true,
            data: JSON.stringify({"order_clientId" : 114, "order_start_dt" : "2016-01-01T21:25:22", "order_end_dt" : "2016-01-05T21:25:22"}), 
            contentType: 'application/json; charset=utf-8',
            type:"POST",
            dataType:"json",
          })
          .done(function(json){
            alert(json);
          });
    });

});