【发布时间】: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}),
-
您的问题与格式错误有关。正如我所看到的,它可能是在 error 或 success 函数中返回的数据。在添加到 json 文档之前检查它们的结果。
标签: javascript jquery asp.net json javascriptserializer