简体   繁体   English

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

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

I am working on simple CRUD with react and react-redux and having trouble with POST in client side我正在使用 react 和 react-redux 处理简单的 CRUD,并且在客户端使用 POST 时遇到问题
BoardList.js 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)

reducers/boards.js减速器/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;
    }
}

actions/boards.js动作/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)));
}

在此处输入图片说明

I know this error message is related to django rest framework, however I can POST it through the Postman.我知道此错误消息与 django rest 框架有关,但是我可以通过 Postman 发布它。 So I think it is my Content-Type issue.(If I am wrong, please correct me) At very first time, I thought django rest framework pass json, so I used application/json .所以我认为这是我的Content-Type问题。(如果我错了,请纠正我)第一次,我认为 django rest 框架通过 json,所以我使用了application/json then I got 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. The submitted data was not a file. Check the encoding type on the form. I also used multipart/form-data and undefined then I get Multipart form parse error - Invalid boundary in multipart: None error.我还使用了multipart/form-dataundefined然后我得到Multipart form parse error - Invalid boundary in multipart: None error。

How can I handle this issue?我该如何处理这个问题?

    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.

相关问题 AJAX 错误:“内容类型中没有多部分边界参数” - AJAX error: "No multipart boundary param in Content-Type" XMLHttpRequest multipart / form-data:多部分中的边界无效 - XMLHttpRequest multipart/form-data: Invalid boundary in multipart 设置Content-Type标头为多部分? - Set Content-Type header to multipart? 下载内容类型为 Content-Type:multipart/mixed 的文件 - Downloading a file with content type Content-Type:multipart/mixed PHP + JS:如何以HTML形式将文件上传为Content-Type Multipart(通过JS)? - PHP+JS: How to do Fileuploads in HTML Form as Content-Type Multipart (via JS)? 使用Javascript(或Angular)在每个部分上使用不同的Content-Type编写multipart / form-data - Composing multipart/form-data with a different Content-Type on each parts with Javascript (or Angular) 如何提取通过 multipart/form-data 发送的文件的 Content-Type - How to extract the Content-Type of a file sent via multipart/form-data 如何从multipart / form-data正文请求中删除Content-Type? - How to remove Content-Type from multipart/form-data Body Request? 在XHR中使用multipart / form-data作为Content-Type时获得&#39;400 Bad Request&#39; - Getting '400 Bad Request' when using multipart/form-data as Content-Type in XHR Axios multipart/form-data 出现在后端,没有内容类型 - Axios multipart/form-data comes up in the backend with no content-type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM