【发布时间】:2020-04-15 23:23:02
【问题描述】:
我正在尝试使用 react 从 Django API 获取响应,但我传递的键值对在响应中不可见。
React 获取代码
handleClick(i) {
.
.
.
if (i != '=') {
.
.
}
else {
// CODE TO FETCH FROM DJANGO API
fetch('http://127.0.0.1:8000/solve/', {
method: 'POST',
body: {"expression":this.state.content}
}).then((response)=>{ console.log(response)})
}
}
Python 代码
# Create your views here.
@api_view(["POST"])
def solveExpression(expression_json):
try:
math_expr = expression_json.data["expression"]
result = eval(math_expr)
data = {"result":result} #This is the data I want to send to reactjs
return JsonResponse(data)
except Exception as e:
return JsonResponse("Error:" + str(e), safe = False)
但不幸的是,我得到的回复中没有关键的“结果”。
请纠正我犯的错误,因为我对 reactjs 很陌生。
【问题讨论】:
标签: json django reactjs python-3.7 react-fullstack