tags: Vue Front and rear separation
Reference link:
Axios encountered the pit when uploading the file, no multipart boundary was found
axios encapsulates use, intercepts specific requests, and determines whether all requests are loaded
const formData = new FormData()
formData.append('role', 1)
formData.append('excelFile', fileObj)
const config = {
headers: {
'Content-Type': 'multipart/form-data'
}
}
const { data: res } = await this.$http.post('admin/user/add', formData, config)
The Content-Type of the request header needs to be set to multipart/form-data (I have solved the problem and checked many articles saying that it does not need to be set. I tried it to no avail. If it is not set, the default type is automatically added instead of the FormData we want. )
note:Don't serialize formData , Post request needs to be serialized (usually qs.stringify()), but not required to pass FormData with post request
If the formData is serialized, the parameters received by the backend will be null (this problem caused me for a long time, only to find out that it was a serialization problem)
Bonus:Determine whether the axios request data is of formData type or ordinary Object
@PostMapping("/admin/user/add")
@ResponseBody
public Map<String, Object> addUserByExcel(@ModelAttribute UserExcelVO userExcelVO) throws Exception {
}
The backend uses a java object to receive parameters
Note: Cross-domain issues
Vue axios sends get and post request parameters get request post request There are two types of post parameters, one is to pass parameters directly, the effect is to encapsulate into a json string, an...
First, introduction axios By default isPayload Format data request, but sometimes the rear-end reception parameter requirements must beForm Data In format, so we have to convert. Payload with Form Dat...
1, front end 2, the back end...
Post request is not the same way as get request to pass parameters Post request: (The parameters in get can be directly in the form of key-value pairs, post needs to convert key-value pairs into query...
Set request header standard notation Set request header Solution 1: (IOS compatibility is problematic, not recommended Solution 2: Use node's qs module, recommended Reference material...
Install the necessary dependencies first POST parameter serialization (add request interceptor) Finally, introduce this axios.js file where needed...
Use the official method of this method, the background can not get the parameters Provide a solution: var params = new URLSearchParams(); params.append('userName', this.userName); params.a...
Since the AXIOS defaults to data, the data format is Request PayLoad, which is not our usual format format, the backend may not be able to get normally, so before sending, you need to use the QS modul...