sooty
sooty

Reputation: 543

jquery how can I convert a json object to javascript array for jqplot

I'm using jqplot to generate charts. It works fine when I define the source data in javascript:

  var goog2 = [["6/22/2009", 425.32],["6/8/2009", 424.84],["5/26/2009", 417.23]];

I'm now trying to pull the data from a server side web method:

  <WebMethod()> _
Public Shared Function Test() As String
    Return ("[[""6/22/2009"",425.32],[""7/22/2009"",429.32]];")
End Function

No matter what I do to the returned object I can't seem to get it into the format that's required.

   function DoAction2(cat) {
          $.ajax({
              type: "POST",
              url: "AjaxTest.aspx/Test",
              data: "{cat:" + cat + "}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
              alert(msg.d);
                  var d = msg.d;
                  plot = $.jqplot('chart1', [d])

Any help would be appreciated.

Upvotes: 1

Views: 851

Answers (2)

Alnitak
Alnitak

Reputation: 339786

Is the returned string from your Test() function the entire response?

In your JS you're asking for msg.d, but there's no d element in your JSON structure (as shown).

Also, the trailing semicolon shouldn't be there. It's not valid JSON.

Upvotes: 1

JAAulde
JAAulde

Reputation: 19550

It seems to me you're returning the exact structure you need from the server, yes? If so, then I don't understand your attempt to access 'msg.d' in your success function. 'msg' is your data structure...

Also, I have no idea what lang you're using on the server side so I don't know much about its syntax, but that does not appear to be properly escaping quotes/etc in order to produce a valid JSON response.

Upvotes: 0

Related Questions