$.ajax 中boolean变成string

1
2
3
var url = 'http://www.test.com/api/test'
var data = {name:'Xiaomi',member:true}
$.post(url, data, () => {})
1
2
//express 服务器接收到的参数
req.body.member === 'true'// true

产生原因是,httpRequest header中 默认是 contentType = application/x-www-form-urlencoded, 如果相传boolean 应该改为 json

1
2
3
4
5
6
7
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: url,
data: JSON.stringify(data),
success: (res) => { }
});