2

I have a javascript variable which looks like:

data = [{
                y: 55.11,
                color: colors[0],
                drilldown: {
                    name: 'MSIE versions',
                    categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],
                    data: [10.85, 7.35, 33.06, 2.81],
                    color: colors[0]
                }
            }, {
                y: 21.63,
                color: colors[1],
                drilldown: {
                    name: 'Firefox versions',
                    categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],
                    data: [0.20, 0.83, 1.58, 13.12, 5.43],
                    color: colors[1]
                }
            }, {
                y: 11.94,
                color: colors[2],
                drilldown: {
                    name: 'Chrome versions',
                    categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0',
                        'Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'],
                    data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22],
                    color: colors[2]
                }
            }, {
                y: 7.15,
                color: colors[3],
                drilldown: {
                    name: 'Safari versions',
                    categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon',
                        'Safari 3.1', 'Safari 4.1'],
                    data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14],
                    color: colors[3]
                }
            }, {
                y: 2.14,
                color: colors[4],
                drilldown: {
                    name: 'Opera versions',
                    categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'],
                    data: [ 0.12, 0.37, 1.65],
                    color: colors[4]
                }
            }];

I want this variable to create from servlet according to data in my database when I'm passing this value from servlet as normal response type (response.setContentType("text/html;charset=UTF-8");) I'm unable to process it. Even if you see this variable its not a valid json object so I can't use JSON.parse.

Please guide me how to get this variable through ajax call what will be dataType in ajax call and what will be response content type in servlet. Or if there is some other way to do it please help. Thanks

2
  • How do you intend to use it on the other side? I see that it is not valid JSON because of colors[N], but if you can't parse it in Javascript, how would you use it anyway? Commented Jun 14, 2012 at 5:43
  • I was wondering same thing as sberry. Commented Jun 14, 2012 at 5:49

3 Answers 3

3

On the servlet side, write the text to a text/plain response:

response.getOutputStream().write(myKindaJsonString.getBytes());

Use jquery's ajax to get it:

$.ajax({ 
           url: CONTEXT_PATH + '/YourServlet',  
           processData: false, 
           type: "GET",               
           success: function(response) {
            // your string is response now
           },
           error: function(response) { 
              alert("There was an error while trying to get value.");
           } 
        });
Sign up to request clarification or add additional context in comments.

Comments

1

Since your data references other objects (colors), thus making it JSON incompatible, why not use eval?

var colors = [1,2,3,4,5,6,7,8];

var data;
eval("data = [{...}]");

...

2 Comments

Thanks Simon it works but while looking one internet people are saying that eval function is deprecated and should avoid its use can u please tell me exactly what could be problem using this funtion.
eval is deprecated as a method of Object, but it's still available in the form above, so no problem there. The risk of eval is that it can be a security problem because you're executing arbitrary code - if someone hijacked your ajax response, they could inject code into your page. You might be safer replacing color = colors[0] with colorIndex = 0, as you could then use JSON. The colorIndex could be reprocessed on the client side using your color[] array.
0

At the server side you can create bean class( or called as DTO objects) with the properties and set the values to it and while sending it back change to the return type as JSON.It will parse to json string and send it back to client side there you can use parseJSON to change from json string to json object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.