繁体   English   中英

多部分表单解析错误 - 多部分中的边界无效:无内容类型问题?

[英]Multipart form parse error - Invalid boundary in multipart: None content-type issue?

我正在使用 react 和 react-redux 处理简单的 CRUD,并且在客户端使用 POST 时遇到问题
BoardList.js

import React, { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { getBoards, deleteBoard, addBoard } from "../../actions/boards"

export class BoardList extends Component {
    static propTypes = {
        boards: PropTypes.array.isRequired,
        getBoards: PropTypes.func.isRequired,
        deleteBoard: PropTypes.func.isRequired,
        addBoard: PropTypes.func.isRequired
    }
    componentDidMount() {
        this.props.getBoards();
    }
    shouldComponentUpdate(nextProps, nextState){
        return this.props.boards !== nextProps.boards
    }
    render(){
        return (
            <Fragment>
                <h2>Boards</h2>
                <table className="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Author</th>
                            <th>Title</th>
                            <th>Created</th>
                            <th>Updated</th>
                            <th />
                        </tr>
                    </thead>
                    <tbody>
                        {this.props.boards.length > 0 && this.props.boards.map(board => (
                            <tr key={board.id}> 
                             // Uncaught TypeError: Cannot read property 'id' of undefined
                                <td>{board.id}</td>
                                <td>{board.author}</td>
                                <td>{board.title}</td>
                                <td>{board.created}</td>
                                <td>{board.updated}</td>
                                <td>
                                    <button 
                                      className="btn btn-danger btn-sm"
                                      onClick={this.props.deleteBoard.bind(this, board.id)}
                                      >
                                      Delete</button>
                                </td>
                            </tr>
                        ))}
                    </tbody>
                </table>
            </Fragment>
        )
    }
}

const mapStateToProps = state => ({
    boards: state.boards.boards,
})

export default connect(mapStateToProps, {getBoards, deleteBoard, addBoard})(BoardList)

减速器/boards.js

import { GET_BOARDS, DELETE_BOARD, ADD_BOARD } from "../actions/types.js";

const initialState = {
    boards : []
}

export default function(state=initialState, action) {
    switch (action.type) {
        case GET_BOARDS:
            return {
                ...state,
                boards: action.payload
            }
        case DELETE_BOARD:
            return {
                ...state,
                boards:state.boards.filter(board => board.id !== action.payload)
            }
        case ADD_BOARD:
            return {
                ...state,
                boards:[...state.boards, action.payload]
            }
        default:
            return state;
    }
}

动作/boards.js


export const addBoard = board => (dispatch, getState) => {
    const token = getState().users.token;
    const config = {
        headers: {
            'Content-Type': undefined   //I suspect this is wrong
        }
    if(token){
        config.headers["Authroization"] = `Token ${token}`
    }
    axios
      .post("api/boards/",board, tokenConfig(getState))
      .then(res=> {
          dispatch(createMessage({ addBoard: "Board added"}));
          dispatch({
              type:ADD_BOARD,
              payload: res.data
          })
      })
      .catch(err => dispatch(returnErrors(err.response.data, err.response.status)));
}

在此处输入图片说明

我知道此错误消息与 django rest 框架有关,但是我可以通过 Postman 发布它。 所以我认为这是我的Content-Type问题。(如果我错了,请纠正我)第一次,我认为 django rest 框架通过 json,所以我使用了application/json 然后我得到了The submitted data was not a file. Check the encoding type on the form. The submitted data was not a file. Check the encoding type on the form. 我还使用了multipart/form-dataundefined然后我得到Multipart form parse error - Invalid boundary in multipart: None error。

我该如何处理这个问题?

    let formData = new FormData()
    formData.append('userPhotos', file)
    formData.append('your_field', your_data)
    formData.append('your_field', your_data)
    formData.append('your_field', your_data)

    axios({
        withCredentials: true,
        url: 'your_url',
        method: 'POST',
        data: formData
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM