0

I am sending JSON data to my django program. I am facing problems decoding the data.

In javascript, I convert an object which encapsulates two arrays into JSON, and read it back.

Javascript:

data = {
    "RegAvStart": reg_start,
    "RegAvStop": reg_end
};
data = $(this).serialize() + "&" + $.param(data);
console.log(data);
var jsondata = JSON.stringify(data);
console.log("JSON:" + jsondata);

Here, both reg_start and reg_end are arrays of strings. In the console I get:

JSON:"&RegAvStart%5B%5D=05%3A00%20AM&RegAvStart%5B%5D=05%3A30%20PM&RegAvStop%5B%5D=08%3A00%20AM&RegAvStop%5B%5D=09%3A30%20PM"

My django(python) code:

def save_doctorslots(request, cliniclabel, doctor_id):
    doctor_id=int(doctor_id)
    doc = get_object_or_404(doctor, docid=doctor_id)
    cl = Clinic.objects.get(label=cliniclabel)
    print("Clinic name", cl.name)
    if request.method == 'POST':
        msg ="Received SaveSlots data for doctor %d clinic %s" % (doctor_id, cliniclabel)
        print(msg)
        print(request.POST)
        regavst = request.POST.get('RegAvStart[]')
        print(type(regavst))
        if not regavst is None:
            regular_available_start = json.loads(regavst)
        else:
            print("Didnt get regular available hours")

Output:

Received SaveSlots data for doctor 1 clinic joelent
<QueryDict: {'RegAvStart[]': ['05:00 AM', '05:30 PM'], 'RegAvStop[]': ['08:00 AM', '09:30 PM']}>
<class 'str'>
2018-10-04 10:45:53,000 django.request ERROR    Internal Server Error: /clinic/joelent/doctor/save/slots/1
Traceback (most recent call last):
  File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/joel/.local/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/joel/myappointments/clinic/views.py", line 261, in save_doctorslots
    regular_available_start = json.loads(regavst)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2 (char 1)
1
  • Try to avoid the stringify function. That is try this, var jsondata = data; Commented Oct 4, 2018 at 5:39

1 Answer 1

2

There is no need for JSON here at all. You have already serialized the data so it is in form-encoded format; doing JSON.stringify just adds another pointless level of serialization. Remove that line and send data directly in your Ajax function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.