Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用axios发布对象列表?_Reactjs_Axios - Fatal编程技术网

Reactjs 使用axios发布对象列表?

Reactjs 使用axios发布对象列表?,reactjs,axios,Reactjs,Axios,我试图发布一个Axios对象列表,但我无法让它工作。我正在使用React前端和Django(python)后端 我想发布这样的数据: [ { invitee: "..." party: "..." }, { invitee: "..." party: "..." }, ... ] 我的第一个想法是将数组作为axios的数据属性: const res = await authAxios.post(`/conversations/invitation

我试图发布一个Axios对象列表,但我无法让它工作。我正在使用React前端和Django(python)后端

我想
发布这样的数据:

[
  {
    invitee: "..."
    party: "..."
  }, {
    invitee: "..."
    party: "..."
  },
  ...
]
我的第一个想法是将数组作为axios的数据属性:

const res = await authAxios.post(`/conversations/invitations/`, toAPIArr);
我还尝试将
JSON.stringify
数组
,但是在后端我总是收到一个错误,说我没有发送列表。我正在做一个简单的检查(这是python):

这里是否有明显的错误?

编辑:我正在后端使用django rest framewok:

查看

class PartyList(APIView):
    def post(self, request, format=None):
        print(isinstance(request.data, list))
        serializer = PartySerializer(data=request.data, many=True)
        user = request.user
        if serializer.is_valid():
            instances = serializer.save(creator=user)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
序列化程序

class PartySerializer(ModelSerializer):
    creator = ReadOnlyField(source='creator.uuid')
    class Meta:
        model = Invitation
        fields = (
            'invitee',
            'party',
        )
        read_only_fields = (
            'uuid',
        )

这个问题有一个答案实际上被否决并删除了,这为我提供了一个可行的解决方案

可以使用axios发送对象:

const res = await authAxios.post(`/conversations/invitations/`, {list: toAPIArr});
然后可以从后端访问此列表(这是python):


这是一个有点黑客,但到目前为止,它为我工作。如果有一个正确的方法(暗示这可能是错误的方法)这样做,也许会有人分享它

请讲一讲。你看过这个请求了吗?尝试了另一个客户端?你能添加你的python代码吗?嗯,上面的代码不是一个最小的可复制示例吗?我正在尝试发送一个以axios为数据的列表。到目前为止,我不知道简单地传递数组是否是实现这一点的方法。或者像这样传递数组是否正确,问题在于api需要不同的格式……我添加了python代码。
const res = await authAxios.post(`/conversations/invitations/`, {list: toAPIArr});
arr = request.data.get('list')