错误: JSON parse error: Unrecognized token ‘roomId‘: was expecting (‘true‘, ‘false‘ or ‘null‘)

今天在使用ajax向后台传输数据的时候突然抛出JSON parse错误:
错误: JSON parse error: Unrecognized token ‘roomId‘: was expecting (‘true‘, ‘false‘ or ‘null‘)_第1张图片
错误提示信息是指回传的数据JSON格式错误, 所以有几种排错思路:

  • 检查json格式数据是否正确, 有没有缺少逗号或者其他符号
  • dataType: "JSON" 有没有写错
  • 回传的数据必须是字符串形式的 JSON 串, 不能是下面这样的:
let data'= {
     "name": "张三",
     "age": 18,
     "sex": 1
 };

应该使用JSON.stringify(data) 将数据转换一下再传入:

$.ajax({
    url: url + '/save-ajax',
    type: 'post',
    dataType: "JSON",
    contentType: "application/json;charset=utf-8",
    data: JSON.stringify(data),
    success: function (data) {
        layer.msg(data.msg, {
            icon: 1,
            time: 1000
        });
    }
});

你可能感兴趣的:(问题)