http中的 contentType & dataType

1 axios 中又一个bug 在get的时候 不会自动带 contentType ,这样会导致服务端识别的时候一些问题

解决办法

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

// 添加请求拦截器
axios.interceptors.request.use((config) => {
if (config.method == 'get') {
config.data = true
config.headers['Content-Type'] = 'application/json'
}
return config
}, (error) => {
// 对请求错误做些什么
console.log(error)
return Promise.reject(error)
})

2 jquery ajax 服务端返回的数据如果是嵌套格式,需要指定dataType: 'json', 如果服务端设置了特定返回格式

function getPromo(obj, fn) {
console.log(JSON.stringify(obj));
$.ajax({
url: '/api/getPromotion.htm',
contentType: 'application/json',
type: 'POST',
dataType: 'json',
data: JSON.stringify(obj),
success: function (ret) {
if (fn) {
fn(ret)
}
}
});

猜你喜欢

转载自www.cnblogs.com/WhiteHorseIsNotHorse/p/10027473.html
今日推荐