ajax 中文乱码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax 中文乱码相关的知识,希望对你有一定的参考价值。
ajax 中文乱码:
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,cn.com.dqhl.model.TBDevice"%>
<!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">
<script type="text/javascript" src="scripts/user.js"></script>
<title>Demo01</title>
</head>
<body>
<p>测试数据</p>
请输入要alert的值:
<br />
<input type="button" id="select" value="查询"/>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
user.js
var req;
window.onload=function()
document.getElementById("select").onclick=select;
;
/*
* 在添加用户时,检查用户名是否存在,使用 ajax 提交请求
**/
function select()
var modelId="LC15";
var layerName="化工建筑装置";
createRequest();
var url = "check?layerName="+layerName+"&modelId="+modelId;
req.open("GET",url,true);
req.onreadystatechange=function()
if(req.readyState == 4)
if(req.status == 200)
alert(req.responseText);
;
req.send(null);
//创建 XMLHttpRequest 对象
function createRequest()
try
// 基于 netsScape 核心的浏览下创建(firefox )
req = new XMLHttpRequest();
catch(e)
try
//IE 5.5 之前的版本创建
req = new ActiveXObject("Msxml2.XMLHTTP");
catch(ee)
try
// IE 5.5 及之后的版本创建
req = new ActiveXObject("Microsoft.XMLHTTP");
catch(eee)
//浏览器不支持 ajax
alert("您的浏览器不支持 ajax");
-----------------------------------------------------------------------------------------------------------
servlet:
package cn.com.dqhl.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.dqhl.model.TBDevice;
import cn.com.dqhl.service.TBDeviceService;
@SuppressWarnings("serial")
public class GetDevice extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
//设置编码方式
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
//从ajax中取值
String modelId=req.getParameter("modelId");
String layerName=null;
modelId=new String(modelId.getBytes("iso-8859-1"),"utf-8");
layerName=new String(req.getParameter("layerName").getBytes("iso-8859-1"),"utf-8");
System.out.println(layerName);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
-------------------------------------------------------------
乱码 不好使啊 求助
url=encodeURI(url);
url=encodeURI(url);
读的时候用这个
String layerName=request.getParameter("layerName");
layerName= java.net.URLDecoder.decode(layerName,"UTF-8");
我都是这样处理的,试试吧,记得给分追问
大哥 不好使 啊
追答把页面编码 换成GBK试试
chrome 开发者工具中中文乱码,ajax请求返回的json串也不能自动格式化了,详见截图,求告知怎么设置...
参考技术A参考下面百度经验解决:
Chrome浏览器如何格式化查看JSON数据 http://jingyan.baidu.com/article/a3a3f811326b128da2eb8a8b.html#5827690-tsina-1-78800-fe183374908e783f9dbfe7dcb7ed2bb5
我要解决是的乱码问题?
以上是关于ajax 中文乱码的主要内容,如果未能解决你的问题,请参考以下文章