views:

93

answers:

1

Hi

Environment used: ASP.NET, jQuery

I have the following AJAX call:

var tempVar = JSON.stringify({plotID:currentId});            

$.ajax({
       type: "POST",
       url: "testPage.aspx/getPlotConfig",
       data: tempVar,
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function(msg) {
       $('#xDimLbl').text(msg.xDim);
       $('#bDimLbl').text(msg.bDim);
       } 
});

The code behind has the method getPlotConfig(string plotID) defined as

public static string getPlotConfig(string plotID)
{
      string x = "T1";
      string b = "T2";
      return Json(new { xDim= x, bDim= b });
}

Questions:

  1. When I do a build, I get the error: The name 'Json' does not exist in the current context Which namespace is amiss?
  2. Along with the two strings x and b, I would like to return a hash table whose key is a string and value is a list of comma separated strings. How do I do that and how to access each key value pair at the client side?

cheers

+1  A: 

This could be referring to the Json method used in ASP.NET MVC controller. As your getPlotConfig function is static you cannot use this method. You may take a look at PageMethods. Here's an example:

[WebMethod]  
[ScriptMethod]
public static object getPlotConfig(string plotID)  
{  
    var hash = new Dictionary<string, string>() 
    {
        { "key1", "valueA,valueB" },
        { "key2", "valueC,valueD" },
    };
    var x = "T1";
    var b = "T2";
    return new { xDim = x, bDim = b, hash = hash };
}

And in javascript:

success: function(msg) {
   $('#xDimLbl').text(msg.d.xDim);
   $('#bDimLbl').text(msg.d.bDim);
   for(var key in msg.d.hash) {
       var value = msg.d.hash[key];
       // Do something with key and value...
    }
}
Darin Dimitrov
Thanks Darin. The build works fine after I used the Serializer. However, I'm not able to read the values in jQuery i.e. $('#xDimLbl').text(msg.xDim); and $('#bDimLbl').text(msg.bDim);. Do I need to deserialize msg before interpreting its elements?
Andriyev
Do you get some error message? Could you post the exact JSON response from the server?
Darin Dimitrov
Did you enable page methods by including a script manager in your aspx page: `<asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />`
Darin Dimitrov
Yes, I have included the script manager inside the form tag and I don't see any explicit errors. I'm using IE8 and haven't really used its Developer Tools to fetch the JSON response. I will try to fetch the JSON response some how.
Andriyev
I did an alert(msg.xDim); and alert(msg.bDim); inside success: function(msg) {...} and the alert shows 'undefined'.
Andriyev
@Andriyev, I've updated my post with a simplified example. You don't need to use `JavaScriptSerializer` manually,it will automatically be invoked when you call the PageMethod in order to JSON serialize the resulting object. All you need to modify is the return type of the method from string to object. Hope this works for you.
Darin Dimitrov
Thanks a bunch Darin. That works perfect.
Andriyev