我想将 bool 值发送到我的 Node.js
服务器。目前,我发送了一个字符串
。我希望 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");
}
});
最佳答案
添加 contentType
应该允许 node.js 解析 bool 值:
$.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");
}
});
关于javascript - 在 Ajax 中发送复选框 True 或 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49543066/