Django API请求是空的,使用R

2024-04-25 13:01:14 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我使用axios来向djangoapi发送JSON请求。在

axios({
  method: 'post',
  url: 'http://127.0.0.1:8000/' + this.state.dataRequestEndPoint,
  data: (reqData)
});

在axios呼叫之前,我可以看到请求:

^{pr2}$

但是,到了Django:

class TargetClass(APIView):
  def get(self, request):
     Here request is empty:
       (Pdb) request.data
       <QueryDict: {}>
       (Pdb) request.body
       b''
  def post(self):
      pass

我做错什么了?在

p.S.也尝试用fetch发送请求:

fetch('http://127.0.0.1:8000/' + this.state.dataRequestEndPoint, {
  method: 'POST',
  body: reqData,
})

都不管用。在


Tags: selfhttpdatarequestdefbodyfetchthis
1条回答
网友
1楼 · 发布于 2024-04-25 13:01:14

下面是解决上述问题的方法。显然,axios需要发送参数:

var reqData = this.state.requestParams
  axios({
    method: 'post',
    url: 'http://127.0.0.1:8000/' + this.state.dataRequestEndPoint,
    params: {
      'a': reqData['a'],
      'b': reqData['b']
    }
  })
  .then(function (response) {
    console.log(response)
  })
  .catch(function (error) {
    console.log(error)
  });

然而,我不知道这个解决方案在安全方面有多好。在

相关问题 更多 >