jQuery JSON发布:传入的对象无效,应为“:"或“}" [英] jQuery JSON Posting: Invalid object passed in, ':' or '}' expected

查看:531
本文介绍了jQuery JSON发布:传入的对象无效,应为“:"或“}"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

我正在工作ckeditor,这样我就可以从ckeditor获取数据.

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;
}

我尝试按以下方式发布从ckeditor捕获的HTML

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

<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

这样,我发布数据.这是代码

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

推荐答案

我会在Ajax请求之前执行此操作:

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;

然后:

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

这比弄乱所有这些引号要容易得多.

This would be easier than messing with all these quotes.

这篇关于jQuery JSON发布:传入的对象无效,应为“:"或“}"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆