json.loads不适用于AngularJS在Python

时间:2015-11-27 17:02:04

标签: python angularjs json django python-3.x

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

在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);
        });
};

在views.py

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

什么都不打印!

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

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 个答案:

答案 0 :(得分:2)

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

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