因为英文在参数传递转换两边格式一样所以没问题.
而中文状态下的解析会出现构造组合问题.出现类似繁体的乱玛.
可以这样解决:
在js中把要传递的参数先经过 encodeURIComponent(var)这个函数转换.
再接收的PHP中用 $var=iconv("UTF-8","GB2312",$_GET['var'])把接受的变量转换成中文.
最后在PHP传递出参数的时候 用函数 $var=iconv("GB2312","UTF-8","Your message")转换一次.问题就解决了.
下面的例子是写在struts2框架下面的。验证邮箱是否已经存在,和数据库交互那部分没有贴上,省略了
-------------------------------------------------------------------------------------------------------
jsp部分:
<%@page contentType="text/htmlcharset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>用户注册</title>
<script type="text/javascript" src="../js/jquery-1.4.3.js"></script>
<script type="text/javascript" >
$(function(){
$("#txtEmail").bind("blur",function(){
var email = $.trim($("#txtEmail").val())
// ajax验证邮箱——开始
$.ajax({url:"validateEmail.action?email="+email,type:"get",dateType:"json",success:function(obj){
if(obj.rely){
alert("邮箱可用")
}else{
alert("邮箱已占用")
}
}})
// ajax验证邮箱——结束
}
})
</script>
</head>
<body>
<div class="fill_message">
<form name="ctl00" method="post" action="register.action" id="f">
<table class="tab_login" >
<tr>
<td valign="top" class="w1">
请填写您的Email地址:
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
-------------------------------------------------------------------------------------------------------
struts2配置文件部分:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="struts-user" extends="dang-default" namespace="/user">
<action name="validateEmail" class="com.tarena.user.action.ValidateEmailAction">
<result type="json" name="jsonSuccess"></result>
</action>
</package>
</struts>
-------------------------------------------------------------------------------------------------------
struts2的action部分:
public class ValidateEmailAction {
private String email
private boolean rely
public String execute(){
UserDao userDao = DaoFactory.getUserDao()
User user = userDao.findUserByEmail(email)
if(user == null){
rely = true
}else{
rely = false
}
return "jsonSuccess"
}
public String getEmail() {
return email
}
public void setEmail(String email) {
this.email = email
}
public boolean isRely() {
return rely
}
public void setRely(boolean rely) {
this.rely = rely
}
}
-------------------------------------------------------------------------------------------------------
var a = new Object()//创建一个对象$.ajax({
type : 'GET',
url : 'b.php',
data : 'name=123&age=12',
success : function(data){ //主要还是看data返回的是什么数据类型,我下面以返回JSON为例子,
var dataArray = jQuery.parseJSON(data)//接受一个JSON字符串,返回解析后的对象。
a = dataArray
console.log(a)
}
})