Java 对servlet的Ajax请求调用不';我什么也不退

Java 对servlet的Ajax请求调用不';我什么也不退,java,jquery,ajax,servlets,Java,Jquery,Ajax,Servlets,这是我的jquery函数。它应该向servlet传递两个值,并返回一个更新值。我检查了值是否正确,两个变量是否正确填写。不幸的是,我没有得到任何回报 $("#passengers").change(function(event){ var passengers = $("#passengers").val(); var price = $("#price").text(); $.getJSON('pricer', {passengers: passenger

这是我的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()在浏览器控制台中打印数据。

尝试:

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()在浏览器控制台中打印数据。

尝试:

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()在浏览器控制台中打印数据。

尝试:

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()

或者使用
get

$.getJSON('pricer', {passengers: passengers, price: price}, function(data){
    $("#price").html(data.price_update);
});
$.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);
});
使用jquery
getJSON()
而不是
get()

或者使用
get

$.getJSON('pricer', {passengers: passengers, price: price}, function(data){
    $("#price").html(data.price_update);
});
$.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);
});
使用jquery
getJSON()
而不是
get()

或者使用
get

$.getJSON('pricer', {passengers: passengers, price: price}, function(data){
    $("#price").html(data.price_update);
});
$.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);
});
使用jquery
getJSON()
而不是
get()

或者使用
get

$.getJSON('pricer', {passengers: passengers, price: price}, function(data){
    $("#price").html(data.price_update);
});
$.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);
});


你在哪里有退货方法?你指的是哪种退货方法?在servlet中?是的,我说的是servlet doPost方法。doPost是否需要返回方法?我想他的意思是您没有在doPost中写入响应。您在方法中返回了什么?您指的是哪种返回方法?在servlet中?是的,我说的是servlet doPost方法。doPost是否需要返回方法?我想他的意思是您没有在doPost中写入响应。您在方法中返回了什么?您指的是哪种返回方法?在servlet中?是的,我说的是servlet doPost方法。doPost是否需要返回方法?我想他的意思是您没有在doPost中写入响应。您在方法中返回了什么?您指的是哪种返回方法?在servlet中?是的,我说的是servlet doPost方法。doPost是否需要返回方法?我想他的意思是您没有在doPost中写入响应。我发布了修改后的servlet@Mazzy-好的,让我在我的系统中尝试一下,然后返回给您。@Mazzy-仅通过发送简单的
string
数据来尝试,而不是先发送
Gson
通过
response.getWriter().write(“Hello”)看这是你想要的,我发布了修改后的servlet@Mazzy-好的,让我在我的系统中尝试一下,然后返回给您。@Mazzy-仅通过发送简单的
string
数据而不是
Gson
来尝试,然后通过
response.getWriter()检查它是否有效。write(“Hello”)看这是你想要的,我发布了修改后的servlet@Mazzy-好的,让我在我的系统中尝试一下,然后返回给您。@Mazzy-仅通过发送简单的
string
数据而不是
Gson
来尝试,然后通过
response.getWriter()检查它是否有效。write(“Hello”)看这是你想要的,我发布了修改后的servlet@Mazzy-好的,让我在我的系统中尝试一下,然后返回给您。@Mazzy-仅通过发送简单的
string
数据而不是
Gson
来尝试,然后通过
response.getWriter()检查它是否有效。write(“Hello”)看这是你想要的,