' STR'对象不可调用django angular2

时间:2017-04-19 19:54:02

标签: angular django-rest-framework

使用angullar2和django休息框架。我试图从角客户端发布一个字符串:

 postAuthCode(code: string) { 
   let headers = new Headers({ 'Content-Type': 'application/json' });
   let options = new RequestOptions({ headers: headers });

   var body ={ "code" : code};

   return this.http
     .post(this.authCodeUrl, {body},options)
     .toPromise()
     .then(res => {  
        console.log("the post response", res );
                 return res ;
     })
    .catch(this.handleError);
}

后端的视图类:

 class AdwordAuthCode(APIView):
    def post(self, request, format=None):

    """
     :param request:
    :param format:
    :return: the credentials from  the recieved code
    """
    data = request.body('code')

    print('data', data)

    return Response(data)

当我在客户端测试时,我得到了

enter image description here

1 个答案:

答案 0 :(得分:2)

正如错误所说,您将request.body视为一个函数,而它是一个字符串。作为回报,你会得到一个500错误的HTML页面。

request.body('code')替换为request.data['code'],应该这样做。