Click here to Skip to main content
15,889,699 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

Hi ,

I am using ajax jason in my application developed in .net 2.0.When i migrated to .net 4.0 the ajax function returning a jason object as [Object Object]
C#
$.ajax({
        type: "POST",
        async: false,
        timeout: 500,
        url: "../PageMethod.aspx/Sample",
        data: '{"Id":"'+Id+'"}',
        contentType: "application/json",
        dataType: "json",
        success: function(result)
        {
        alert(" Before Result "+result);
            document.getElementById(panelName).innerHTML = result;
            alert("Result After  "+result);
        }
    });

[WebMethod] public static string Sample(int Id)
 { string msg; StringBuilder sb = new StringBuilder(); sb.Append("<table cellspacing=\"3\" cellpadding=\"3px\">"); //Morecode sb.Append("</table>"); return sb.ToString(); 
}

Can any one tell me where I am doing mistake.
Thanks
Prashant
Note: When I hard code document.getElementById(panelName).innerHTML = 'HHHH';its working fine
Posted
Updated 16-Jan-13 23:01pm
v2
Comments
[no name] 17-Jan-13 4:26am    
please add the method too (PageMethod.aspx/Sample).
Prashant Bangaluru 17-Jan-13 4:58am    
[WebMethod]
public static string Sample(int Id)
{
string msg;
StringBuilder sb = new StringBuilder();
sb.Append("<table cellspacing=\"3\" cellpadding=\"3px\">");
//Morecode
sb.Append("</table>");


return sb.ToString();
}

In your code
C#
alert(" Before Result "+result); // use result.d
alert(" Before Result "+result.d)// use this line 
// same every where 
            document.getElementById(panelName).innerHTML = result.d;
            alert("Result After  "+result.d);


Hope this will help up
 
Share this answer
 
Comments
Prashant Bangaluru 21-Jan-13 4:36am    
Many Thanks Bro..Its working perfectly...What is that .d does??
solanki.net 21-Jan-13 6:15am    
your welcome , .d is used to get data from object .
Prashant Bangaluru 22-Jan-13 0:19am    
Hmmm..Thanks for helping at the right time..I am still confused how it was working in .net 2.0 with the same code..
what you are returning from the method is a html string not a json string which is expected by the ajax call (
JavaScript
contentType: "application/json",dataType: "json",
)

So you have two choices 1) change the sample method to return as a json string or 2) remove the
JavaScript
contentType
and
JavaScript
dataType
from your ajax method - jquery by default infers the correct type, in this case html.

hope this solves your problem.
 
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