得到?在提醒ajax响应或将其发布到文本字段时

时间:2015-02-23 06:55:41

标签: java jquery ajax

我已经在jquery中为我的服务器创建了一个ajax调用,我现在面临的问题是我的响应是打印了吗?即使将正确的整数值写入输出流。 Ajax函数如下。

$dntb.on('click', 'button', function(event) {
                var i = $(this).closest('tr').index(); //have to get the row where the button is clicked 
                var sditmId = $("#sditm").val();
                var sdhedId = $("#sdhed").val();
                $.get('getstock', {
                    sditmId: sditmId,
                    sdhedId: sdhedId
                }, function(response) {
                    alert(response);
                    var stk = ""+response;
                    $("#stk").val(stk);
                });
            });

点击下表中显示的问题按钮调用此功能

Issue button in display notes page

服务器代码如下所示

int stk = null;
switch (userPath) {
        case "/getstock":
            stk = opo.getStockData(request.getParameter("sditmId") request.getParameter("sdhedId")); //value to write into the output stream.
            break;
        case "/temp":
            //er = opo.checkCatUniqueForEdit(request.getParameter("catName"), request.getParameter("catId"));
            break;
    }
System.out.println(stk); //Printing correctly
    response.setContentType("text/html");
    response.getWriter().write(stk);

获取值的代码

public int getStockData(String sditm, String sdhed) {
    int stk = 0;
    try {
        String query = "Select stk.Stk_instk from tbstk stk inner join tbsditm itm on itm.Sditm_prdid=stk.Stk_prdid where itm.Sditm_sdhed=" + sdhed + " and itm.Sditm_id=" + sditm;
        Statement stmt = dcon.con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        if (rs.next()) {
            stk = rs.getInt("Stk_instk");
        }
    } catch (SQLException ex) {
        Logger.getLogger(Op_OrdConf.class.getName()).log(Level.SEVERE, null, ex);
    }
    return stk;
}

ajax调用成功发生,但当我提醒响应时,我正在获取并且我在服务器中正确获取值,但在客户端则是?请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

不确定,但似乎你在这里错过了一个逗号:

stk = opo.getStockData(request.getParameter("sditmId"), request.getParameter("sdhedId"));
//----------------------------------------------------^-----i think this is missing.

根据您的评论,我建议您明确将dataType设置为html

$.get('getstock', {
  sditmId: sditmId,
  sdhedId: sdhedId
}, function(response) {
  alert(response);
  var stk = "" + response;
  $("#stk").val(stk);
},"html"); //<----------add the dataType here.

答案 1 :(得分:0)

我将从下面给出的函数返回的int值转换为String,它似乎是内容类型和写入输出流的值之间的一些冲突

stk = opo.getStockData(request.getParameter("sditmId") request.getParameter("sdhedId"));