[英]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-data
和undefined
然后我得到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.