我想通过 AJAX post 将不同的数字作为数字而不是字符串发送。我知道,ajax 请求/http 协议(protocol)不是类型安全的,因此我创建了一个 JSON 对象,但在服务器端它仍然以字符串形式接收。
var node = $(this);
var id = parseInt(node.data("id"));
var isActive = node.is(':checked') ? 1 : 0;
var jsonParam = {Id: id, IsActive: isActive};
$.ajax({
url: "/accounts/edit-account",
type: "POST",
data: jsonParam,
dataType: "json",
success: function (response)
{
},
error: function (xhr) {
alert("Error while updating the account");
}
});
返回:
{ Id: '102', IsActive: '1' }
服务器端代码:
router.post('/edit-account', function(req, res) {
console.log(req.body)
});
最佳答案
尝试在 AJAX 中指定 contentType: "application/json"
。 dataType
指定响应中期望的内容,而不是请求中的内容。
关于javascript - Ajax 请求以字符串形式发送数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38286998/