Solve the problem that the backend of vue+axios post parameters cannot be received

Use axios to send a post request. The type of 'Content-Type' in the HTTP request hearer is 'application/json;charset=UTF-8', which cannot be received by the backend. If the backend uses the springmvc framework, you can make the corresponding method The parameter can be received by adding the annotation @RequestBody, but this method is not friendly. After this change, other places need to call for access. If the parameter is not passed in json, it cannot be used.

   Next, consider solving the problem from the previous paragraph, first install the qs module of nodejs: npm install qs --save

Then modify the post code:

var qs = require('qs');
function post(url, params){
	return axios.post(url, 
		qs.stringify(params),
		{
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			}
		}).then(res => res.data);
}
export const addSysTable = params => { return post(`/api/table/insert.do`, params).then(res => res.data);};

In this way, the parameters can be passed normally when addSysTable is called.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325572645&siteId=291194637