4

I have an asp.net application with a static page method. I'm using the below codes to call the method and get its returned value.

$.ajax({
       type: "POST",
       url: "myPage/myMethod",
       data: "{'parameter':'paramValue'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function(result) {alert(result);}                                
 });

What i got returned is [object Object].

Below is my static method. And I also have EnablePageMethods="true" EnablePartialRendering="true" in my ScriptManager.

    [WebMethod]
    [ScriptMethod]
    public static string myMethod(string parameter)
    {
         return "Result";
    }

Is there a way for me to get the returned value?

3
  • When you update the post to include the question, if it is regarding the return value please include the method you are calling too Commented Jun 24, 2011 at 3:43
  • 1
    You have an extra closing bracket on your alert call. Not knowing your question, this could be your problem. alert(result)); Commented Jun 24, 2011 at 3:49
  • 3
    unless you are using asp.net 2.0 or less you are stuck at the d parameter. encosia.com/never-worry-about-asp-net-ajaxs-d-again Commented Jun 24, 2011 at 4:14

4 Answers 4

6

Try using Chrome developer tools or the firebug plugin from Firfox. Not sure if IE's developer tools lets you inspect the ajax calls?

The resulting string you are looking for is actually within the result object. You need to look at the d variable. I remember reading somewhere why this was, I think it is ASP.NET playing around :|

Try:

success: function(data) {alert(data.d);} 

c#

[WebMethod]
public static string GetTest(string var1)
{
    return "Result";
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

data.d is key here, asp.net serves up json in .d for some security reasons
just found that out few mins after editting the question.LOL. jquery is awesome! loving it.
its not asp.net playing around. .d is there for a reason :)
4

Its just that you are stuck at the .d that is introduced in the JSON response from ASP.NET 3.5. To quote Dave Ward,

If you aren’t familiar with the “.d” I’m referring to, it is simply a security feature that Microsoft added in ASP.NET 3.5’s version of ASP.NET AJAX. By encapsulating the JSON response within a parent object, the framework helps protect against a particularly nasty XSS vulnerability.

So just check whether .d exists and then unwrap it. Change your success function like this.

success: function(result) {
    var msg = result.hasOwnProperty("d") ? result.d : result;
    alert(msg );
}        

Comments

0

What about this?

$.ajax({
     type: "POST",
     url: "myPage/myMethod?paramater=parameter",
     success: function(result) {
        alert(result);
     }                                
 });

Comments

0

I found out the solution.

You can use parseJSON to get the result http://api.jquery.com/jQuery.parseJSON/

or change the datatype to html to see the actual value. http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

Thank you guys for your help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.