【问题标题】:jQuery JSON Posting: Invalid object passed in, ':' or '}' expectedjQuery JSON 发布:传入的对象无效,应为 ':' 或 '}'
【发布时间】:2017-11-15 07:15:01
【问题描述】:

当我尝试将 JSON 发布到服务器端函数时,我收到此错误

传入的对象无效,应为“:”或“}”

我正在使用 ckeditor,通过这种方式我从 ckeditor 获取数据。

var ckEditorCtrl = GetClientID("CKEditor1").attr("id");
var newcontent = getEditorContents(ckEditorCtrl.toString());

function GetClientID(id, context) {
    var el = $("#" + id, context);
    if (el.length < 1)
        el = $("[id$=_" + id + "]", context);
    return el;
}

function getEditorContents(ename) {
    if (CKEDITOR.instances[ename])
        return CKEDITOR.instances[ename].getData();

    var e = $("textarea[id$='" + ename + "']")[0];
    if (e)
            return e.value;

    return false;
}

我试图从 ckeditor 发布捕获的 HTML 如下

<img alt="" src="https://shop.bba-reman.com/wp-content/uploads/2017/05/Toyota-Auris-gearbox-actuator-1-300x300.jpg" style="width: 300px; height: 300px;" /><br />
<br />
We can <strong>REPAIR </strong>your Toyota Auris gearbox actuator

我通过这种方式发布数据。这是代码

$.ajax({
    type: "POST",
    url: "/abcpage.aspx/contentinsert",
        //data: '{"CID":"' + $("[id$='txtContentID").val() + '","CTitle":"' + $("[id$='txtTitle").val() + '","CDesc":"' + $("[id$='txtDesc").val() + '","CKey":"' + $("[id$='txtKeywords").val() + '","CBody":"' + newcontent + '"}',
        data: '{"CID":"' + $("#txtContentID").val() + '","CTitle":"' + $("#txtTitle").val() + '","CDesc":"' + $("#txtDesc").val() + '","CKey":"' + $("#txtKeywords").val() + '","CBody":"' + newcontent + '","OldBody":"' + oldcontent + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        InsertSuccess(msg);
        ComboLoad();
        HideProgressAnimation();

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        var jsonError = JSON.parse(XMLHttpRequest.responseText);
        alert(jsonError.Message);
        ComboLoad();
        HideProgressAnimation();
    }
});

【问题讨论】:

  • 将字符串连接到您的数据并分配给 var stringData="CID:"+ $("#txtContentID").val() 之类的变量,然后执行以下数据: JSON.stringify( {stringData}),
  • 您的问题与格式错误有关。正如我所看到的,它可能是在 errorsuccess 函数中返回的数据。在添加到 json 文档之前检查它们的结果。

标签: javascript jquery asp.net json javascriptserializer


【解决方案1】:

我会在 Ajax 请求之前这样做:

var data = {};

data.CID = $("#txtContentID").val();
data.CTitle = $("#txtTitle").val();
data.CDesc = $("#txtDesc").val();
data.CKey = $("#txtKeywords").val();
data.CBody = newcontent;
data.OldBody = oldcontent;

然后:

$.ajax({
  data: JSON.stringify(data),
  // ...

这比搞乱所有这些引号要容易。

【讨论】:

    【解决方案2】:

    传入的对象无效,应为“:”或“}”

    JavaScriptSerializer 抛出的ArgumentException,ASP.NET 使用它来反序列化 JSON。该错误意味着您的 JSON 格式错误。例如,可能存在杂散引号或缺少花括号。

    我们可以使用这个简单的程序来复制错误,该程序试图反序列化一个 JSON 字符串,其中包含一个额外的错误双引号:

    void Main()
    {
        var js = new JavaScriptSerializer();
        string invalidJson = "{\"Testing\":\"\"test\"}";
        js.Deserialize<Test>(invalidJson);
    }
    
    public class Test
    {
        public string Testing { get; set; }
    }
    

    上述抛出具有相同的错误消息。有效的 JSON 不会产生任何错误:

    string invalidJson = "{\"Testing\":\"test\"}";
    

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多