2017-04-19 18 views
1

正在使用angullar2 Django的REST框架。我試圖從角客戶端發表的字符串:'STR' 對象不是可調用的Django angular2

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

回答

2

由於錯誤說,你在治療request.body的功能,而這是一個字符串。作爲回報,你會得到一個500錯誤的HTML頁面。

通過request.data['code']更換request.body('code')和應該做的。

+1

是的,我做到了,它的工作感謝您的答案 – sila