Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am passing error message as json from handler(.ashx).but in ajax its getting as string only.please help me to get it as json value.

handler.ashx

C#
msg = "{";
                msg += string.Format("error:'{0}',\n", "Protected");
                msg += string.Format("msg:'{0}'\n","protected value.");
                msg += "}";

context.Response.Write(msg);



in mypage.aspx

C#
success: function (data, status) {
                           var obj = JSON.stringify(data);
                           if (typeof (data.error) != 'undefined') {
                               if (data.error != '') {



here data is coming as string instead of json .so data.error is getting as undefined.please help to rectify the issue.
Posted
Comments
[no name] 8-Jul-14 12:41pm    
your purpose is not clear to us
josh-jw 9-Jul-14 1:20am    
how to convert string to json

1 solution

Need to convert string data in to object using eval() method after that use to JSON.strigfiy()

For example
JavaScript
var result = eval(data);
var obj = JSON.stringify(result);


It might help you
Convert data into JSON[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900