json.loads不适用于AngularJS在Python中发布的二进制JSON

时间:2021-12-11 06:58:38

I am coding in Django and my front-end uses Angularjs like this:

我在Django编码,我的前端使用像这样的Angularjs:

In controller.js

$scope.reserve = {
    reserve: {
        duration: {
            startTimeUnix: null,
            startTime: "صبور باشید...",
            endTime: null
        },
        service: null,
        reserver: {
            first_name: null,
            last_name: null,
            phone: null
        }
    },
    block: {
        duration: null
    },
    is_block: false,
    taken_time: null,
    staff: "صبور باشید..."
};

$scope.submitAdminTime = function() {
    $http.post("/schedule/admin/block/", {
            data: $scope.reserve
        }, {
            "headers": {
                "Content-Type": "application/json"
            }
        })
        .success(function(data) {
            console.log(data);
        });
};

In views.py

@csrf_exempt
def admin_block_time(request):
    try:
        json_object = json.loads(request.body)
        print('yes')
    except ValueError:
        print('no')

Prints nothing!

什么都不打印!

I don't know why json.loads() doesn't work at all! I posted another question here and followed up all the answers.

我不知道为什么json.loads()根本不起作用!我在这里发布了另一个问题并跟进了所有答案。

request.body

b '{
"reserve": {
    "duration": {
        "startTimeUnix": "",
        "startTime": "\xd8\xb5\xd8\xa8\xd9\x88\xd8\xb1 \xd8\xa8\xd8\xa7\xd8\xb4\xdb\x8c\xd8\xaf...",
        "endTime": "\xdb\xb1\xdb\xb3\xdb\xb9\xdb\xb4/\xdb\xb0\xdb\xb9/\xdb\xb0\xdb\xb4        \xdb\xb1\xdb\xb2:\xdb\xb2\xdb\xb0",
        "endTimeUnix": 1448441400
    },
    "service": null,
    "reserver": {
        "first_name": "\xd9\x85\xd8\xb1\xdb\x8c\xd9\x85",
        "last_name": "\xd9\x85\xd9\x88\xd8\xb3\xd9\x88\xdb\x8c",
        "phone": "09124955173"
    }
},
"block": {
    "duration": null
},
"is_block": false,
"taken_time": null,
"staff": "alireza",
"service": [
    "O5KLFPZB"
]
}
'

1 个解决方案

#1


2  

In Python 3, you need to decode the bytes into a string before you can load the json.

在Python 3中,您需要在加载json之前将字节解码为字符串。

json.loads(request.body.decode('utf-8'))

#1


2  

In Python 3, you need to decode the bytes into a string before you can load the json.

在Python 3中,您需要在加载json之前将字节解码为字符串。

json.loads(request.body.decode('utf-8'))