jQuery ajax get请求编码问题,jQuery ajax简化处理,jQuery ajax与Servlet交互

jsp代码

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<base href="http://localhost:8081/JSPproject/">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
	$(but).click(function(){
		$.ajax({
			url : "AjaxServlet" ,
			method : "post" ,
			data : {
				msg : $(msg).val() ,
				info : "hello" ,
			} ,
			dataType : "text" ,
			success : function(data){
				alert(data) ;
			} ,
			error : function(){
				alert("error") ;
			}
		}) ;
	}) ;
}) ;
</script>
</head>
<body>
	<input type="text" id="msg">
	<input type="button" id="but" value="按钮"> 
</body>
</html>


java代码

 

 

package me1.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AjaxServlet
 */
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String code = "UTF-8" ;
		request.setCharacterEncoding(code);
		response.setCharacterEncoding(code);
		String msg = request.getParameter("msg") ;
		String info = request.getParameter("info") ;
		
		response.getWriter().println(msg) ;
		response.getWriter().println(info+" world!!!");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}

接收json数据

ajax改为

 

$.ajax({
	url : "AjaxServlet" ,
	method : "post" ,
	data : {
		msg : $(msg).val() ,
		info : "hello"
	} ,
	dataType : "json" ,
	success : function(data){
		alert(data.msg) ;
	} ,
	error : function(){
		alert("error") ;
	}
}) ;


Servlet改为

 

 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String code = "UTF-8" ;
        request.setCharacterEncoding(code);
        response.setCharacterEncoding(code);
        String msg = request.getParameter("msg") ;
        String info = request.getParameter("info") ;
        String jsonStr = "{\"msg\":\"" + msg + "\",\"info\":\"" + info +"\"}" ;
        System.out.println(jsonStr);
//        response.getWriter().println(msg) ;
//        response.getWriter().println(info+" world!!!");
        response.getWriter().println(jsonStr) ;
    }}

 

ajax简化处理

 

post请求

 

$.post("AjaxServlet",{msg : $(msg).val() ,info : "hello"},function(data){
	alert(data.msg) ;
},"json") ;

get请求

 

 

$.get("AjaxServlet",{msg : $(msg).val() ,info : "hello"},function(data){
	alert(data.msg) ;
},"json") ;

其实两种方法是一样的

 

get请求要注意编码问题

两种修改方式

  1. 修改tomcat中的server.xml,tomcat的t默认编码方式是ISO-8859-1,要改为UTF-8。在server.xml里Connector节点中添加属性URIEncoding="UTF-8"
    <Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
    server.xml里有多个Connector节点,只要修改protocol="HTTP/1.1"的即可。
  2. 字符串编码转换。字符串.getBytes("ISO-8859-1"), "UTF-8");
    String msg = new String(request.getParameter("msg").getBytes("ISO-8859-1"), "UTF-8");
    感觉这样改不好,这样一来,换成post又乱了。

     

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值