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