Dennis
Dennis

Reputation: 1156

jquery get json and returns first as a label?

I have Google'd this thing to death, lots of people are having the same problem but the common fix isn't working for me..

My data which is returned from my home server:

{
  "errors": 1.15,
  "allErrors": null,
  "threads": 10.83,
  "sale": 131.36,
  "delivery": 1.68,
  "failed": 60,
  "webErrors": 432,
  "webErrorsByMin": 0
}

I have tried:

            $.ajax({
                type: "GET",
                url: "http://srv3.localhost:8080/monitor/Totals?callback=?",
                dataType: "jsonp",
                success: function(data) {
                    var items = [];
                    $.each(data, function(key, val) {
                        items.push('<li id="' + key + '">' + val + '</li>');
                    }); 
                    }
            }, "jsonp");

I have also tried:

$.getJSON("http://srv3.localhost:8080/monitor/Totals?callback=?",
                    function(data){

                        var result = eval( "(" + data + ")" );
                });

It keeps trying to set the first key as a label..

Firefox error:

invalid label
"address": 1.8, 

I have tried so many different things but always it comes back as a label.. After my google searches, the common fix was to encapsulate the return using:

var result = eval( "(" + data + ")" );

But its not working in my case.. :(

Anyone know why this isn't working for me? Using jquery 1.4.2

Thanks!

Upvotes: 0

Views: 629

Answers (4)

Lia Veja
Lia Veja

Reputation: 11

For Tomcat servlet side, you need a filter to tranforme the response. How to program a filter, you can find in Tomcat side examples. A good starting point is:

http://www.java2s.com/Tutorial/Java/0400__Servlet/Filterthatusesaresponsewrappertoconvertalloutputtouppercase.htm

Upvotes: 1

Abdul Munim
Abdul Munim

Reputation: 19217

You are returning JSON from your server and assuming you are doing a cross-domain call and that's why you need a JSONP request.

You need to wrap your JSON data into a function that you passed through parameter callback.

Your JavaScript code seems to be quite fine but your server code needs to be doing something like this:

HandleRequest()
BEGIN
   callbackFunction = REQUEST.PARAMETER["callback"]
   RESPONSE.WRITE callbackFunction + "(" + jsonSerializedData + ")"
END

PS: You don't need to do JSONP request if you are requesting on the same server.

Upvotes: 0

mericano1
mericano1

Reputation: 2913

did you try this ?

for (var key in data){
  items.push('<li id="' + key + '">' + data[key] + '</li>');
}

Upvotes: 0

Scruffy The Janitor
Scruffy The Janitor

Reputation: 472

I believe the results get obfuscated to data.d.

You can verify this by taking a look at the JSON return using Firebug and doing a net capture of the request and response.

Upvotes: 0

Related Questions