admin管理员组文章数量:1415491
Jquery
$('#PostComment').click(function () {
var mentTitle = $('#mentTitle').val();
var mentClob = $('#mentClob').val();
var id = $('#topicId').val();
var buttoname = $('#PostComment').val();
var obj;
$.ajax({
type: "post",
url: "../../Handler/Topic.ashx",
data: "mentclob=" + mentClob + "&menttitle=" +
mentTitle + "&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 method
CommentModel cm = daoobject.populateCommentModel(listmentsbytopic);
var json= cm.CreateCommentJson();
context.Response.Write(json);
Function definition
public string CreateCommentJson()
{
// serialize the names to JSON
var jss = new JavaScriptSerializer();
var json = jss.Serialize(this);
return json;
}
I am getting output 2 alert boxes
first is
{ "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)/" }
and second is
Incorrect undefined undefined
Can anyone help.
Jquery
$('#PostComment').click(function () {
var mentTitle = $('#mentTitle').val();
var mentClob = $('#mentClob').val();
var id = $('#topicId').val();
var buttoname = $('#PostComment').val();
var obj;
$.ajax({
type: "post",
url: "../../Handler/Topic.ashx",
data: "mentclob=" + mentClob + "&menttitle=" +
mentTitle + "&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 method
CommentModel cm = daoobject.populateCommentModel(listmentsbytopic);
var json= cm.CreateCommentJson();
context.Response.Write(json);
Function definition
public string CreateCommentJson()
{
// serialize the names to JSON
var jss = new JavaScriptSerializer();
var json = jss.Serialize(this);
return json;
}
I am getting output 2 alert boxes
first is
{ "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)/" }
and second is
Incorrect undefined undefined
Can anyone help.
Share Improve this question edited Apr 8, 2012 at 8:21 coder25 asked Mar 31, 2012 at 23:40 coder25coder25 2,39314 gold badges65 silver badges108 bronze badges 2- we need more information on what you're trying to do. This is an ajax request to an 'ashx' page. From the looks of it, the returned data is malformed... run through jsonlint. to see what I mean. – g19fanatic Commented Apr 6, 2012 at 18:55
- Parse error on line 1: "{ \"UserId\": \ ^ Expecting '{', '[' is what am getting after validating – coder25 Commented Apr 7, 2012 at 9:02
4 Answers
Reset to default 4 +50Your returned json is malformed. You should not be escaping all of the "
as you are.
Run your json response msg
through http://jsonlint. and then run this one through it.
{
"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)/"
}
Follow on:
In your ments, you state that you are getting the first alert printed out. If you are getting it printed out as you say, then that shows that the data is still in string format and NOT already parsed as json. If it were already parsed as json, you'd be getting an [Object object] message instead of the actual text.
That being said, check out this jsFiddle and this resulting of a Firebug breakpoint.
This essentially shows that the .parseJSON() function will properly parse this new 'json string'. From what you've given us, it tells me that you haven't given us everything. You are doing something (possible typo?, more processing that isn't getting posted?, something!) to that string (if in fact you are getting it as stated) before you parse it with JQuery.
Use obj.CommentClob
instead of obj[0].CommentClob
as the json is not an array..
Additionally you can give another option dataType: 'json'
to the .ajax()
call and have jQuery handle the parsing.. (or use $.getJSON()
directly)
Your json is not an array it is a key value pair object.
There is a run time error on this line alert("Correct" + obj[0].CommentClob)
. Change this line to alert("Correct" + obj.CommentClob);
As a side note, in order to get the error message from exception object(e) use e.message
or e.description
and for error number use e.number
. Since JavaScript is case sensitive e.Description
or e.Number
will give you undefined value.
Working demo - http://jsfiddle/MuEYt/
I think you are parsing listmentsbytopic to json twice.
first time here:
CommentModel cm = daoobject.populateCommentModel(listmentsbytopic);
var json= cm.CreateCommentJson();
context.Response.Write(json);
second time here:
obj = jQuery.parseJSON(msg);
above line is not needed
when you are sending json data from server why are you not using
dataType: 'json'
in below function
$('#PostComment').click(function () {
var mentTitle = $('#mentTitle').val();
var mentClob = $('#mentClob').val();
var id = $('#topicId').val();
var buttoname = $('#PostComment').val();
var obj;
$.ajax({
type: "post",
url: "../../Handler/Topic.ashx",
data: "mentclob=" + mentClob + "&menttitle=" +
mentTitle + "&topicId=" + id + "&Button=" + buttoname,
dataType: 'json', /// this one expect data in json format
success: function (msg) {
try {
alert(msg);
obj = jQuery.parseJSON(msg);
alert("Correct" + obj.CommentClob);
}
catch (e) {
alert("Incorrect" + e.Description + e.ErrorNumber);
}
}
});
return false;
});
});
本文标签: javascriptUndefined Error in parsing JSON in ajax function in NET MVCStack Overflow
版权声明:本文标题:javascript - Undefined Error in parsing JSON in ajax function in .NET MVC - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745182097a2646512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论