在我的项目中,前端位于Angular2, 我正在向这样的Django网址发出POST请求:
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify(this.myMetadata);
let req = this.http.post(this.url,body,options)
.map((res:Response) => res.json())
.subscribe(
data => { this.response = data},
err => console.error(err),
() => console.log('done')
);
return req;
但是Django后端没有将数据接收为JSON
if request.method == 'POST':
logger.info(request.method)
logger.info(type(request.body))
logger.info(request.POST)
日志转储显示Django已经将数据作为字符串接收,因为没有任何json方法可以使用它。
Method:POST
request.body type:<type 'str'>
解决此问题的最佳方法是什么?
有没有办法将字符串转换为字典?
答案 0 :(得分:0)
有没有办法将字符串转换为字典?
是的,只需使用json模块
import json
...
if request.method == 'POST':
data = json.loads(request.body)
logger.info(type(data))