[Solved] Python Django Parse JSONP (Json with Padding)

I’m using JSONP to send data from otherdomain.com to mydomain.com

However, I got errer when I tried to parse JSONP data in django controller (views.py).

Here is my code.

Javascript in mydomain.com client page

jsonData = {
    'foo': 'bar',

    url: 'http://otherdomain.com/end_point/',
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    data: jsonData,
    dataType : 'jsonp',
    jsonp : 'callback'

Django controller (views.py) in mydomain.com

from django.http import JsonResponse
import json

def decode_jsonp(request):

    if 'callback' in request.GET:
        json_object = json.loads(request.body) # Raise error
        json_object = json.dumps(request.body) # Do not raise error but returns nothing
        json_object = json.loads(json.dumps(request.body)) # Raise error

        foo = json_object['foo']

    return JsonResponse({'foo': foo})

I don’t know what should I substitue for json_object = json.loads(request.body)

Read more here: Source link