如何将值从Jquery传递给jsp?

时间:2010-11-27 07:01:38

标签: javascript jquery ajax jsp

我的jQuery代码:

function check_availability(){   

         //get the username   
         alert("hai");
         var result ="";
         var CompanyCode = $('#CompanyCode').val();   
     alert("Jquery " +CompanyCode);
     //use ajax to run the check   
         $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   
             function(result){   
                 //if the result is 1   
                 alert("result"+result);
                 if(result == 1){   
                     //show that the username is available   
                     $('#username_availability_result').html(CompanyCode + ' is Available');   
                 }else{   
                     //show that the username is NOT available   
                     $('#username_availability_result').html(CompanyCode + ' is not Available');   
                 }   
         });   

}

checkUserName.jsp:

String companyCode = request.getParameter("CompanyCode");
out.println("companyCode"+companyCode);

rs = dbAccess.executeQuery("select companycode from yosemitecompany where companycode = '"+companyCode+"'");

如何从jQuery获取值?我需要在JSP页面中获取答案,以检查表中是否存在记录。我没有从request.getParamater() method获得价值。这有其他方法可以检查吗?

3 个答案:

答案 0 :(得分:2)

而不是

     $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   

尝试

     $.post("checkUserName.jsp", { Code: CompanyCode },   

将密钥命名为与var ...

相同的问题也许是个问题

答案 1 :(得分:0)

您的代码绝对有效。只有如上所述的变量命名可能是问题。 您可以尝试jquery api中的其他选项,

$ .post(“checkUserName.jsp”,$('#CompanyCode')。serialize(),..

这应该照顾你的问题。因为查询字符串值应该正确引用('')。这种序列化方法会解决这个问题。

答案 2 :(得分:0)

似乎CompanyCode是id属性。所以只需创建一个属性 name =“something” 然后使用request.getParameter(“something”)。我希望它能正常工作