在通过AJAX发送JSON时,我遇到了一些非常奇怪的行为。通过AJAX发送JSON时出现奇怪的行为
JSON数据:
data = {
"name": "box1",
"comment": ["fragile"],
"type_A": [
{
"item": "1",
"attr": [
"big",
"red"
]
},
{
"item": "2",
"attr": [
"small",
"red"
]
},
],
"type_B": [],
"misc": {}
};
POST请求:
$.ajax({
url: url,
type: "POST",
// data: JSON.stringify(data),
data: data,
success: function (result) {
console.log("inside success");
},
error: function (error) {
console.log("inside error");
}
});
如果我通过在这里data
没有JSON.stringify()
,空场type_B
和misc
迷路/剥夺。但是如果我确实使用JSON.stringify()
,那么后端无法正确解析它,没有JSON.parse()
。如果没有在后端执行JSON.parse()
,有没有办法避开空场?
我尝试添加contentType: "application/json"
和dataType: "json"
的请求,但没有帮助。
你是什么意思的“后端无法正确解析它”? – cybersam
在后端使用JSON.parse有什么错误? –
你的后端是什么?我认为你的问题是你的后端! – Lux