admin管理员组

文章数量:1334365

When I am trying to post JSON to server side function then I am getting this error

Invalid object passed in, ':' or '}' expected

I am working ckeditor and this way I am getting data from 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;
}

The HTML I am trying to post capture from ckeditor as follows

<img alt="" src=".jpg" style="width: 300px; height: 300px;" /><br />
<br />
We can <strong>REPAIR </strong>your Toyota Auris gearbox actuator

This way I am posting data. Here is the code

$.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();
    }
});

When I am trying to post JSON to server side function then I am getting this error

Invalid object passed in, ':' or '}' expected

I am working ckeditor and this way I am getting data from 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;
}

The HTML I am trying to post capture from ckeditor as follows

<img alt="" src="https://shop.bba-reman./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

This way I am posting data. Here is the code

$.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();
    }
});
Share Improve this question edited Mar 13, 2018 at 18:39 Stephen Kennedy 21.6k24 gold badges97 silver badges112 bronze badges asked Jun 13, 2017 at 13:16 Monojit SarkarMonojit Sarkar 2,45111 gold badges47 silver badges98 bronze badges 3
  • Do string concatenate to your data and assign to a variable like var stringData="CID:"+ $("#txtContentID").val() and then do like this data: JSON.stringify({stringData}), – Venu Commented Jun 13, 2017 at 13:26
  • Your problem is about a bad formar. As i can see it could be at the data wich is returned in error or success function. Check them results before add to json document. – Jorge Omar Medra Commented Jun 13, 2017 at 13:33
  • for people who came here by searching for "Invalid object passed in, ':' or '}' expected", like me: this can also happen from // ments. Most things ignore them, but some strictly adhere to JSON standards and don't, then return the invalid object error message. – Steve Commented Jun 10, 2024 at 10:47
Add a ment  | 

2 Answers 2

Reset to default 3

I would do this before the Ajax request :

var data = {};

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

Then:

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

This would be easier than messing with all these quotes.

Invalid object passed in, ':' or '}' expected

is an ArgumentException thrown by the JavaScriptSerializer used by ASP.NET to deserialize JSON. The error means that your JSON is malformed. There may for example be a stray quote or a missing curly brace.

We can replicate the error with this simple program which tries to deserialize a JSON string which has an extra erroneous double quote in it:

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

public class Test
{
    public string Testing { get; set; }
}

The above throws with the same error message. Valid JSON does not produce any error:

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

本文标签: javascriptjQuery JSON Posting Invalid object passed in3939 or 3939 expectedStack Overflow