Ajax JSON 添加尾随冒号

ajax json adding trailing colon

本文关键字:JSON 添加 Ajax      更新时间:2023-09-26
var mydata = {
    "one": {
        "brand": "acer",
        "cost": "212132"
    }
}

$.ajax({
        url: 'http://localhost',
        type: 'post',
        dataType: 'json',
        data: JSON.stringify(mydata)
    });

上面的代码是在Chrome开发工具中查看表单发送数据时向数据添加冒号。请求有什么问题吗?

您正在查看 application/x-www-form-urlencoding 解析的数据。因此,Chrome正试图建立键值对。单击网络选项卡视图中"表单数据"标题附近的"查看源"。您将看到不带冒号的 JSON。

对我来说

是一样的。我在查看源代码时没有看到冒号。但它没有用。我的解决方案是添加缺少的contentType

$.ajax({
        url: `url`,
        contentType: "application/json",
        type: "POST",
        data: JSON.stringify(data)
  });