我想将bool发送到我的Node.js
服务器。目前,我发送了string
。我希望bool
作为bool
发送,而不是字符串。对我来说似乎应该是;但是,在我的mongodb
数据库中,它存储为字符串。提前谢谢!
$.ajax({
type: "POST",
url: "url",
timeout: 2000,
data: {
'bool': $('input[name=bool]').is(':checked')
},
success: function(data) {
$('#Error').css('display', 'none')
$('#Error').html('')
$('#Good').css('display', 'block')
$('#Good').html('Success! Sending information and redirecting...')
},
error: function(jqXHR, textStatus, err) {
//server error
console.log("bad server");
}
});
答案 0 :(得分:1)
添加contentType
应该允许node.js解析布尔值:
$.ajax({
type: "POST",
url: "url",
contentType: "application/json",
timeout: 2000,
data: {
'bool': $('input[name=bool]').is(':checked')
},
success: function(data) {
$('#Error').css('display', 'none')
$('#Error').html('')
$('#Good').css('display', 'block')
$('#Good').html('Success! Sending information and redirecting...')
},
error: function(jqXHR, textStatus, err) {
//server error
console.log("bad server");
}
});
答案 1 :(得分:0)
解决了这个问题,因为有些人说我需要将string
转换为boolean
服务器端。我通过var BoolName = (BoolName == bool);
实现了这个目标。现在,这会将MongoDB
保存为boolean
类型。