JSONP breaks at two points (" : ")

Asked

Viewed 191 times

2

The server sends a JSON, normal. Follows the code:

@GET
@Path("email")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String getEmail(){
    ViewStatisticsEmail statisticsEmail = new ViewStatisticsEmail();
    statisticsEmail.setDoacoes(1432);
    statisticsEmail.setLidas(5443);
    statisticsEmail.setNaoLidas(4667);
    statisticsEmail.setSent(10000);
    statisticsEmail.setVisualization("Atualizado por ultimo " + Calendar.getInstance().getTime());
    return new Gson().toJson(statisticsEmail);
}

And I’m using ajax to draw a graph with this information. Follow the code of ajax:

function chartEmail() {
    var url = 'http://192.168.0.42:8080/Admin/service/email';
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'jsonp',
        success: function (data) {
            var data = google.visualization.arrayToDataTable([
                ['Task', 'valores'],
                ['Lidas',     data.lidas],
                ['Não lidas',      data.naoLidas],
                ['Doações',  data.doacoes]
            ]);

            var options = {
                title : data.sent,
                legend : "bottom",
                pieHole: 0.4
            };

            var chart = new google.visualization.PieChart(document.getElementById('chart-email'));
            chart.draw(data, options);
        }
    });
  }

It is the google Harts.

Here comes the following mistake: Uncaught SyntaxError: Unexpected token:

E o JSON quebra assim

  • Why jsonp? This looks like a normal JSON... (no padding)

  • So... that’s the problem, if I put normal json it gives "no Access control allow origin

  • This is because the server does not allow external connections. The server and this page are in fact domain?

  • Sergio is localhost the two... plus and testing, when going to production will no longer be... as I do for the server enable external connections?

  • If you change your jsonp for json, that answer here help you?

  • gave another error SERIOUS: Servlet.service() for Servlet [Jersey REST Service] in context with path [/Admin] threw Exception [Servlet Execution threw an Exception] with root cause

  • In Java I can not help much, but you can take a look here: http://www.codingpedia.org/ama/how-to-add-cors-support-on-the-server-side-in-java-with-jersey/ and surely someone will come to help in this. However developing uses addresses without http://192.168.0.42:8080, that is relative. This makes the browser realize that ajax is local and no longer blocks with Access control allow origin

  • with jsop it does and receives the response of the request, just can’t read it, as you can see there in the print I sent, it breaks in two points...

Show 3 more comments

1 answer

1


Since in javascript you expect a JSONP from the server, then the server has to send a JSONP, not a JSON:

@GET
@Path("email")
@Produces("application/javascript")
@Consumes(MediaType.APPLICATION_JSON)
public String getEmail(@QueryParam("callback") String callback) {
    ViewStatisticsEmail statisticsEmail = new ViewStatisticsEmail();
    statisticsEmail.setDoacoes(1432);
    statisticsEmail.setLidas(5443);
    statisticsEmail.setNaoLidas(4667);
    statisticsEmail.setSent(10000);
    statisticsEmail.setVisualization("Atualizado por ultimo " + Calendar.getInstance().getTime());
    String json = new Gson().toJson(statisticsEmail);
    return callback + "(" + json + ")";
}

This code is almost equal to your original code. The differences are:

  • @Produces("application/javascript") - After all, what it produces now is a javascript snippet, not a JSON.

  • @QueryParam("callback") String callback - The parameter of callback is essential for JSONP to work.

  • return callback + "(" + json + ")"; - Do not just return the JSON, you need to put it inside the callback call.

Relevant source: https://oneminutedistraction.wordpress.com/2014/02/25/using-jsonp-with-jax-rs/

  • Vlw man, I was after this solution since Saturday! was very useful...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.