Avatar of Des Kelly
Des Kelly
Flag for Ireland asked on

JQPlot reading from webservice

Hi,

I have been trying to get a jqplot chart (a pie chart, in this instance) to work using a C# webservice as the data.  I have tried returning data in all sorts of formats, but it is always being interpreted by JQuery as an object, and I cannot get it to transform into anything useful.  The below works perfectly when I either hard code the data value, or pull it from a text file (for example [[['first', 8], ['second', 26], ['third', 12]]];), but obviously this isn't all that useful.  

Here's the jquery:

  $.ajax({
        type: "POST",
        url: "WEBSERVICEURL",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            plot1 = $.jqplot('pie1', JSON.parse(response.toString(), {
                gridPadding: { top: 0, bottom: 38, left: 0, right: 0 },
                seriesDefaults: { renderer: $.jqplot.PieRenderer, trendline: { show: false }, rendererOptions: { padding: 8, showDataLabels: true} },
                legend: { show: true, placement: 'outside', rendererOptions: { numberRows: 1 }, location: 's', marginTop: '15px' }
            });
        },
        failure: function (errMsg) {
            $('#errorMessage').text(errMsg);  //errorMessage is id of the div
        }
    });

I have tried the following return types in the webservice:
String, List<CustomObject>, Array, JSON String (formatted as per normal JSON response)

When I do an alert(response) in the JQuery above, I get <object>, and the errMsg just says "object XMLHTTPRequest".

I am new to jplot, and am slowly going mad trying to get this to work, so any advice would be gratefully received.
JavaScriptAJAXWeb Development

Avatar of undefined
Last Comment
Des Kelly

8/22/2022 - Mon
Bardobrave

JSON is interpreted by JavaScript as an object, thus probably is why you are receiving [object] when you try to alert the data that the webservice return to you.

Try alerting one of its attributes to see if the JSON you are receiving is correctly formatted and interpreted.

i.e.
alert(response.gridPadding.top)

For further comprehension on JSON and JavaScript check this (it helped me a lot):
http://www.json.org/js.html
ASKER CERTIFIED SOLUTION
Des Kelly

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Des Kelly

ASKER
The solution provided did not help to resolve the issue.

I eventually figured out the solution myself which I have since posted to the question for others to review should the need arise.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23