用ajax把jsp里输入的数据传入servlet不成功?

心疼我的过去丶 2015-12-08 04:34:02
Jsp里的代码:
<script type="text/javascript">
$(function (){
$.ajax({
url:"Action",
type:"post",
dataType:"json",
timeout:5000,
data:{
action:"Action",
tel:$("#tel").val()
},
async:false,
success:function(data){
$('#tbList').empty();
var users = data.users;
for(i=0;i<users.length;i++){
var user = users[i];
$('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
+user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
+user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
}
},
error:function(){
//服务器出错之后的处理逻辑
alert("error");
}
});
});
</script>
<body>
<input id="tel" name="tel"/> <button onclick="window.location='Action'">查询</button>
</body>

Servlet里的代码:
String tel = (String)request.getAttribute("tel");
System.out.println("tel="+tel);

但是我运行时这里输出的tel=null;这是为什么啊?请教下a会ajax的大神啊
...全文
192 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 9 楼 u012651054 的回复:
[quote=引用 7 楼 u010540940 的回复:] [quote=引用 6 楼 u012651054 的回复:] [quote=引用 4 楼 u010540940 的回复:] [quote=引用 1 楼 u012651054 的回复:] 你在点击查询的时候,是申请了新的页面,这个页面重新初始化了<input id="tel" name="tel"/> 它的值肯定是空的,你如果想要ajax实现这个功能 在<button onclick="window.location='Action'">不能调用window.location 而是要直接调用ajax方法才行。 可以这样调整:

<script type="text/javascript">
           $(function (){ 
        	   $("#btn_search").click(function(){
        	   	$.ajax({
                            url:"Action",
                            type:"post",
                            dataType:"json",
                            timeout:5000,
                            data:{
                                action:"Action",
                                tel:$("#tel").val()
                            },
                            async:false,
                            success:function(data){
                                $('#tbList').empty();
                                var users = data.users;
                                for(i=0;i<users.length;i++){
                                    var user = users[i];
                                    $('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
                                            +user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
                                            +user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
                                }
                            },
                            error:function(){
                                //服务器出错之后的处理逻辑
                                alert("error");
                            }
                        });
        	   });
                    
}); 
</script>
<body>
   <input id="tel" name="tel"/> <button  id = "btn_search">查询</button>
</body>
改了还是没用啊,servlet里面输出的获取值还是null。。
String tel = (String)request.getAttribute("tel");
//String tel = request.getParameter("tel");这两种都没用
[/quote] 作为post或者get的请求,发送到request中的参数,不能通过getAttribute获得,而是要通过getParameter()获取。[/quote] 都试了,还是没用。唉,我还是老老实实用我的表单提交吧。。[/quote] 虽然不知道你是怎么弄的,我后台是取到了值 后台代码: String tel = request.getParameter("tel"); System.out.println(tel); [/quote] 我后来知道怎么回事了,data:{ action:"Action", tel:$("#tel").val() } 这里不需要那个action,去掉就可以了。
南猿北辙 2015-12-09
  • 打赏
  • 举报
回复
for(i=0;i<users.length;i++){ 这里有错 改为 for(var i=0;i<users.length;i++)
  • 打赏
  • 举报
回复
引用 6 楼 u012651054 的回复:
[quote=引用 4 楼 u010540940 的回复:] [quote=引用 1 楼 u012651054 的回复:] 你在点击查询的时候,是申请了新的页面,这个页面重新初始化了<input id="tel" name="tel"/> 它的值肯定是空的,你如果想要ajax实现这个功能 在<button onclick="window.location='Action'">不能调用window.location 而是要直接调用ajax方法才行。 可以这样调整:

<script type="text/javascript">
           $(function (){ 
        	   $("#btn_search").click(function(){
        	   	$.ajax({
                            url:"Action",
                            type:"post",
                            dataType:"json",
                            timeout:5000,
                            data:{
                                action:"Action",
                                tel:$("#tel").val()
                            },
                            async:false,
                            success:function(data){
                                $('#tbList').empty();
                                var users = data.users;
                                for(i=0;i<users.length;i++){
                                    var user = users[i];
                                    $('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
                                            +user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
                                            +user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
                                }
                            },
                            error:function(){
                                //服务器出错之后的处理逻辑
                                alert("error");
                            }
                        });
        	   });
                    
}); 
</script>
<body>
   <input id="tel" name="tel"/> <button  id = "btn_search">查询</button>
</body>
改了还是没用啊,servlet里面输出的获取值还是null。。
String tel = (String)request.getAttribute("tel");
//String tel = request.getParameter("tel");这两种都没用
[/quote] 作为post或者get的请求,发送到request中的参数,不能通过getAttribute获得,而是要通过getParameter()获取。[/quote] 都试了,还是没用。唉,我还是老老实实用我的表单提交吧。。
小米拌大头菜 2015-12-09
  • 打赏
  • 举报
回复
引用 4 楼 u010540940 的回复:
[quote=引用 1 楼 u012651054 的回复:] 你在点击查询的时候,是申请了新的页面,这个页面重新初始化了<input id="tel" name="tel"/> 它的值肯定是空的,你如果想要ajax实现这个功能 在<button onclick="window.location='Action'">不能调用window.location 而是要直接调用ajax方法才行。 可以这样调整:

<script type="text/javascript">
           $(function (){ 
        	   $("#btn_search").click(function(){
        	   	$.ajax({
                            url:"Action",
                            type:"post",
                            dataType:"json",
                            timeout:5000,
                            data:{
                                action:"Action",
                                tel:$("#tel").val()
                            },
                            async:false,
                            success:function(data){
                                $('#tbList').empty();
                                var users = data.users;
                                for(i=0;i<users.length;i++){
                                    var user = users[i];
                                    $('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
                                            +user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
                                            +user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
                                }
                            },
                            error:function(){
                                //服务器出错之后的处理逻辑
                                alert("error");
                            }
                        });
        	   });
                    
}); 
</script>
<body>
   <input id="tel" name="tel"/> <button  id = "btn_search">查询</button>
</body>
改了还是没用啊,servlet里面输出的获取值还是null。。
String tel = (String)request.getAttribute("tel");
//String tel = request.getParameter("tel");这两种都没用
[/quote] 作为post或者get的请求,发送到request中的参数,不能通过getAttribute获得,而是要通过getParameter()获取。
小米拌大头菜 2015-12-09
  • 打赏
  • 举报
回复
引用 7 楼 u010540940 的回复:
[quote=引用 6 楼 u012651054 的回复:] [quote=引用 4 楼 u010540940 的回复:] [quote=引用 1 楼 u012651054 的回复:] 你在点击查询的时候,是申请了新的页面,这个页面重新初始化了<input id="tel" name="tel"/> 它的值肯定是空的,你如果想要ajax实现这个功能 在<button onclick="window.location='Action'">不能调用window.location 而是要直接调用ajax方法才行。 可以这样调整:

<script type="text/javascript">
           $(function (){ 
        	   $("#btn_search").click(function(){
        	   	$.ajax({
                            url:"Action",
                            type:"post",
                            dataType:"json",
                            timeout:5000,
                            data:{
                                action:"Action",
                                tel:$("#tel").val()
                            },
                            async:false,
                            success:function(data){
                                $('#tbList').empty();
                                var users = data.users;
                                for(i=0;i<users.length;i++){
                                    var user = users[i];
                                    $('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
                                            +user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
                                            +user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
                                }
                            },
                            error:function(){
                                //服务器出错之后的处理逻辑
                                alert("error");
                            }
                        });
        	   });
                    
}); 
</script>
<body>
   <input id="tel" name="tel"/> <button  id = "btn_search">查询</button>
</body>
改了还是没用啊,servlet里面输出的获取值还是null。。
String tel = (String)request.getAttribute("tel");
//String tel = request.getParameter("tel");这两种都没用
[/quote] 作为post或者get的请求,发送到request中的参数,不能通过getAttribute获得,而是要通过getParameter()获取。[/quote] 都试了,还是没用。唉,我还是老老实实用我的表单提交吧。。[/quote] 虽然不知道你是怎么弄的,我后台是取到了值 后台代码: String tel = request.getParameter("tel"); System.out.println(tel);
  • 打赏
  • 举报
回复
引用 2 楼 u011098115 的回复:
是啊,<button onclick="window.location='Action'">查询</button>一般都是给一个ID,然后在JS中的单击事件写ajax查询。
我表示不会这种。。
  • 打赏
  • 举报
回复
引用 1 楼 u012651054 的回复:
你在点击查询的时候,是申请了新的页面,这个页面重新初始化了<input id="tel" name="tel"/> 它的值肯定是空的,你如果想要ajax实现这个功能 在<button onclick="window.location='Action'">不能调用window.location 而是要直接调用ajax方法才行。 可以这样调整:

<script type="text/javascript">
           $(function (){ 
        	   $("#btn_search").click(function(){
        	   	$.ajax({
                            url:"Action",
                            type:"post",
                            dataType:"json",
                            timeout:5000,
                            data:{
                                action:"Action",
                                tel:$("#tel").val()
                            },
                            async:false,
                            success:function(data){
                                $('#tbList').empty();
                                var users = data.users;
                                for(i=0;i<users.length;i++){
                                    var user = users[i];
                                    $('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
                                            +user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
                                            +user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
                                }
                            },
                            error:function(){
                                //服务器出错之后的处理逻辑
                                alert("error");
                            }
                        });
        	   });
                    
}); 
</script>
<body>
   <input id="tel" name="tel"/> <button  id = "btn_search">查询</button>
</body>
改了还是没用啊,servlet里面输出的获取值还是null。。
String tel = (String)request.getAttribute("tel");
//String tel = request.getParameter("tel");这两种都没用
走在四季 2015-12-08
  • 打赏
  • 举报
回复
“data:{ action:"Action", tel:$("#tel").val() },” 为什么不是直接写 data:“tel=”+$("#tel").val(),
走在四季 2015-12-08
  • 打赏
  • 举报
回复
是啊,<button onclick="window.location='Action'">查询</button>一般都是给一个ID,然后在JS中的单击事件写ajax查询。
小米拌大头菜 2015-12-08
  • 打赏
  • 举报
回复
你在点击查询的时候,是申请了新的页面,这个页面重新初始化了<input id="tel" name="tel"/> 它的值肯定是空的,你如果想要ajax实现这个功能 在<button onclick="window.location='Action'">不能调用window.location 而是要直接调用ajax方法才行。 可以这样调整:

<script type="text/javascript">
           $(function (){ 
        	   $("#btn_search").click(function(){
        	   	$.ajax({
                            url:"Action",
                            type:"post",
                            dataType:"json",
                            timeout:5000,
                            data:{
                                action:"Action",
                                tel:$("#tel").val()
                            },
                            async:false,
                            success:function(data){
                                $('#tbList').empty();
                                var users = data.users;
                                for(i=0;i<users.length;i++){
                                    var user = users[i];
                                    $('#tbList').append('<tr><td><input type="checkbox" id="check" name="check"/></td><td>'
                                            +user.name+'</td><td>'+user.number+'</td><td>'+user.type+'</td><td>'
                                            +user.tel+'</td><td>'+'<button onclick="window.location="Look"">查看</button></td></tr>');
                                }
                            },
                            error:function(){
                                //服务器出错之后的处理逻辑
                                alert("error");
                            }
                        });
        	   });
                    
}); 
</script>
<body>
   <input id="tel" name="tel"/> <button  id = "btn_search">查询</button>
</body>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧