熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

js調用AJAX時Get和post的亂碼

2013-11-15 12:15:36  來源: JSP教程 

  在使用"get"時抓取的頁面最後加上編碼類型 

 代碼如下: <%  服務器端servletactioncontextgetresponse()setcharacterencoding("utf");  客戶端 網頁特效p/jsphtml target=_blank >jsp教程 <%@ page language="java" contenttype="text/html; charset=utf" pageencoding="utf"%>  responseexpires =   responseaddheader "pragma""nocache"  responseaddheader "cachectrol""nocache"  responseaddheader "contenttype""text/html; charset=gb"這是重點否則會出現亂碼  responsewrite "中文漢字"%>    在使用post時用vbscript解決了編碼問題  源碼如下   代碼如下: <script language="vbscript">  function urlencoding(vstrin)  strreturn = ""  for i = to len(vstrin)  thischr = mid(vstrini)  if abs(asc(thischr)) < &hff then  strreturn = strreturn & thischr  else  innercode = asc(thischr)  if innercode < then  innercode = innercode + &h  end if  hight = (innercode and &hff) &hff  low = innercode and &hff  strreturn = strreturn & "%" & hex(hight) & "%" & hex(low)  end if  next  urlencoding = strreturn  end function  function bytesbstr(vin)  strreturn = ""  for i = to lenb(vin)  thischarcode = ascb(midb(vini))  if thischarcode < &h then  strreturn = strreturn & chr(thischarcode)  else  nextcharcode = ascb(midb(vini+))  strreturn = strreturn & chr(clng(thischarcode) * &h + cint(nextcharcode))  i = i +   end if  next  bytesbstr = strreturn  end function  </script>    下面是使用vbscript函數   代碼如下: <script language=網頁特效>  /**  * 初始化一個xmlhttp對象  */  function initajax()  {  var ajax=false;  try {  ajax = new activexobject("msxmlxmlhttp");  } catch (e) {  try {  ajax = new activexobject("microsoftxmlhttp");  } catch (e) {  ajax = false;  }  }  if (!ajax && typeof xmlhttprequest!=undefined) {  ajax = new xmlhttprequest();  }  return ajax;  }  function saveuserinfo()  {  var msg = documentgetelementbyid("msg");  var f = documentuser_info;  var username = fuser_namevalue;  var userage = fuser_agevalue;  var usersex = fuser_sexvalue;  var url = "saveasp教程";  var poststr = urlencoding("user_name="+ username +"&user_age="+ userage +"&user_sex="+ usersex);//post時采用編碼傳遞  var ajax = initajax();  ajaxopen("post" url true);  ajaxsetrequestheader("contenttype""application/xwwwformurlencoded");  ajaxsend(poststr);  ajaxonreadystatechange = function() {  if (ajaxreadystate == && ajaxstatus == ) {  msginnerhtml = bytesbstr(ajaxresponsebody); //獲取時解碼  }  }  }  </script>  <form name="user_info">  姓名<textarea name="user_name" /></textarea><br />  年齡<input type="text" name="user_age" /><br />  性別<input type="text" name="user_sex" /><br />  <input type="button" value="提交表單" onclick="saveuserinfo()">  </form>  <div id="msg"></div>  成功     
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20587.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.