繁体   English   中英

对Servlet的Ajax请求调用未返回任何内容

[英]Ajax request call to the servlet doesn't return anything

这是我的jQuery函数。 它应该传递给servlet两个值并获取更新值。 我检查了值是否正确使用以及两个变量是否正确填充。 不幸的是我一无所获。

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

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

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

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

        });

});

这是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);   
}

主要问题是我没有收到任何错误...甚至没有... JavaScript控制台错误为空,甚至servlet也没有显示任何错误

尝试:

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

首先尝试这个简单的方法:

response.getWriter()。write(“ Hello”);

在ajax中:

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

        console.log(data);

    });

然后尝试运行并将其打印出来。

console.log()在浏览器控制台中打印数据。

使用jquery getJSON()代替get()

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

或与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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM