利用ajax向jsp传输数据
ajax代码
var obtn=document.getElementsByTagName('input')[0];
obtn.onclick=function () {
var xhr=null;
try{
xhr=new XMLHttpRequest();
}catch(e){
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
//第一种方法用get get 中由于要编码可以用encodeURI('刘伟')但是不知道为什么出来是问号就在后台想把编码转好了,然后还要注意get中由于会出现缓存的问题,可以随时加个随机数,就是new Date()当时间当做随机数
/*xhr.open('get','send.jsp?name='+'刘伟'+'&date=30&'+new Date().getTime(),true);
xhr.send();*/
//第二种方法是post方法,没有缓存也不用编码
xhr.open('post','send.jsp',true);
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
xhr.send('name=刘伟&date=30');
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
alert(xhr.responseText);
}}}}
jsp后台
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html><body>
<%
String name=request.getParameter("name");
String date=request.getParameter("date");
//转换成一个字节数组,利用iso-8859-1编码 注意这里!因为编码后才能够解决前台输出中文乱码的额问题。
name = new String(na.getBytes("iso-8859-1"),"UTF-8"); //把name转为utf-8编码
out.println("姓名"+name);
out.println("时间"+date);
%>
</body></html>
最新文章
- [转]retina屏下支持0.5px边框的情况
- 托马斯微积分答案.djvu的书签
- UIAlertView&#39; is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlert
- 烂泥:使用snmpwalk采集设备的OID信息
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
- Sublime ctags 函数跳转插件安装
- ecshop网站建设手机版wap版出现lib.debug.php on line 303
- php中的数组遍历的几种方式
- Java数据库连接池比较(c3p0,dbcp,proxool和BoneCP)
- 快速搭建属于自己的数据库——mongodb
- 基于vue-cli构建vue-router的入门级demo
- 利用python执行shell脚本 并动态传参 及subprocess基本使用
- spring boot 2 统一异常处理
- [20190401]关于semtimedop函数调用.txt
- bzoj3514(主席树+lct)
- typedef定义数组类型
- MVC5控制器传值的三种方式(ViewData,ViewBag,TempData),刚刚学习MVC5的新手,希望各位大神多多指教
- ELK 性能(3) — 在 Docker 上运行高性能容错的 Elasticsearch 集群
- redis主从配置(docker实现)
- 【Android】使用Pull生成/解析XML文件