gpt4 book ai didi

javascript - 在 .NET MVC 的 ajax 函数中解析 JSON 时出现未定义错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:53 25 4
gpt4 key购买 nike

Jquery

$('#PostComment').click(function () {

var commentTitle = $('#commentTitle').val();
var commentClob = $('#commentClob').val();
var id = $('#topicId').val();
var buttoname = $('#PostComment').val();
var obj;
$.ajax({

type: "post",
url: "../../Handler/Topic.ashx",

data: "commentclob=" + commentClob + "&commenttitle=" +
commentTitle + "&topicId=" + id + "&Button=" + buttoname,
success: function (msg) {

try {
alert(msg);
obj = jQuery.parseJSON(msg);
alert("Correct" + obj.CommentClob);

}
catch (e) {


alert("Incorrect" + e.Description + e.ErrorNumber);
}



}
});

return false;
});
});

Topic.ashx -ProcessRequest 方法

     CommentModel cm = daoobject.populateCommentModel(listcommentsbytopic);
var json= cm.CreateCommentJson();
context.Response.Write(json);

函数定义

     public string CreateCommentJson()
{
// serialize the names to JSON
var jss = new JavaScriptSerializer();

var json = jss.Serialize(this);
return json;
}

我得到输出 2 个警告框

首先是

 { "UserId": "1", "ToipicId": "44f94c32-c415-4751-812a-03b775775698", "CommentId": "0f1014a0-08d9-48f7-9a0c-d9d6b3d841b2", "CommentClob": "ilikeit", "CommentTitle": "nice", "DescriptionClob": null, "DateCreated": "/Date(1333233498780)/", "Datemodified": "/Date(-62135596800000)/" }

其次是

Incorrect undefined undefined

谁能帮忙。

最佳答案

您返回的 json 格式错误。你不应该像现在一样转义所有的 "

通过 http://jsonlint.com 运行您的 json 响应 msg然后通过它运行这个。

{
"UserId": "1",
"ToipicId": "44f94c32-c415-4751-812a-03b775775698",
"CommentId": "0f1014a0-08d9-48f7-9a0c-d9d6b3d841b2",
"CommentClob": "ilikeit",
"CommentTitle": "nice",
"DescriptionClob": null,
"DateCreated": "/Date(1333233498780)/",
"Datemodified": "/Date(-62135596800000)/"
}

继续:

在您的评论中,您声明您正在打印第一个警报。如果你像你说的那样打印出来,那么这表明数据仍然是字符串格式并且没有被解析为 json。如果它已被解析为 json,您将收到 [Object object] 消息而不是实际文本。

话虽这么说,看看这个 jsFiddle结果 screenshot一个 Firebug 断点。

这基本上表明 .parseJSON() 函数将正确解析这个新的“json 字符串”。从你给我们的东西来看,它告诉我你还没有给我们一切。在使用 JQuery 解析该字符串之前,您正在对那个字符串(如果实际上您按照说明得到它)做一些事情(可能是打字错误?,更多的处理没有被发布?,一些事情!)。

关于javascript - 在 .NET MVC 的 ajax 函数中解析 JSON 时出现未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960654/

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