gpt4 book ai didi

javascript - jQuery JSON 发布 : Invalid object passed in, ':' 或 '}' 预期

转载 作者:行者123 更新时间:2023-11-29 10:05:27 25 4
gpt4 key购买 nike

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

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

我正在使用 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();
}
});

最佳答案

我会在 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),
// ...

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

关于javascript - jQuery JSON 发布 : Invalid object passed in, ':' 或 '}' 预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44522685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com