Mazzy
Mazzy

Reputation: 14227

Ajax request call to the servlet doesn't return anything

This is my jquery function. It should pass to the servlet two value and get back an update value. I checked if the values are taken correctly and the two variables are filled correctly. Unforuntately I don't get nothing back.

 $("#passengers").change(function(event){

    var passengers = $("#passengers").val();
    var price = $("#price").text();

        $.getJSON('pricer', {passengers: passengers, price: price}, function(data){

            $("#price").html(data);

        });

});

here is the servlet

        */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
{
    String passengers = request.getParameter("passengers");
    String price = request.getParameter("price");

    String price_update = new Gson().toJson(this.pricerBean.calculatePrice(Integer.parseInt(passengers), Float.parseFloat(price)));

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(price_update);   
}

The main problem is I don't get any error...even one...the javascript console error is empty and even the servlet doesn't show any errors

Upvotes: 0

Views: 714

Answers (2)

rajesh kakawat
rajesh kakawat

Reputation: 10906

use jquery getJSON() instead of get()

$.getJSON('pricer', {passengers: passengers, price: price}, function(data){
    $("#price").html(data.price_update);
});

or with get

$.get('pricer', {passengers: passengers, price: price}, function(data){
    // also validate data if it is  not blank
    var data = jQuery.parseJSON(data);
    $("#price").html(data.price_update);
});

Upvotes: 1

user3145373 ツ
user3145373 ツ

Reputation: 8156

try :

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(data)); //here data is what you want to send

first try this simple :

response.getWriter().write("Hello");

in ajax :

    $.get('pricer', {passengers: passengers, price: price}, function(data){

        console.log(data);

    });

then try to run and what it prints post me.

console.log() prints data in browser console.

Upvotes: 2

Related Questions