jquery ajax 转码

来源:互联网 发布:python车道线识别 编辑:程序博客网 时间:2024/05/17 03:37

test.html:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
 function DoAjax(){
  $.post("AjaxTest.aspx",{txt:$("#tbox1").val()},
   function(data){
    $("#AjaxResponse").text(data);
   }
  );
 }
</script>
</head>

<body>
<p><a href="javascript:DoAjax();">AjaxTest</a><input name="tbox1" id="tbox1" type="text" /></p>
<div id="AjaxResponse"></div>
</body>
</html>

AjaxTest.aspx:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<script runat="server">
 string parms;
 
 void Page_Load(object sender , EventArgs e)
 {
  parms=Request["txt"];
  if (String.IsNullOrEmpty(parms)){
   Response.Write("Is Null");
  }else{
   Response.Write(parms);
  }
 }
</script>

文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/1_web/javascript/jsjs/20090303/157078.html

问:

var url="http://127.0.0.1:81/test/test.nsf/getsearchResult?openagent";
  function ajax_keyword(url) {
   xmlhttp = createXmlHttpRequest();
   xmlhttp.onreadystatechange = handleSearchSuggest;
   xmlhttp.open("post",url, true);
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=GB2312");
   xmlhttp.send("keyword=" + keyword.value);
  }
结果在代理中获取  send过去的参数“keyword”    中文出现乱码,怎么处理??

答:

首先你的代码有2个错误
一般domino都是用utf-8的格式
并且 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=GB2312");
这一段是post  xml文件才用到,可以去掉

xmlhttp.send("keyword=" + keyword.value);
这里,如果你的 keyword.value 是中文   最好自己转码下 英文不用转码的
改成 xmlhttp.send("keyword=" + encodeURIComponent(keyword.value));

然后在接受的代理
再用 Evaluate 去执行公式用 @URLDecode 对传递进来的参数再次转码

最后 解析出来的数据就OK了

ls中:
dim queryAr as variant

queryAr=Evaluate(|@URLDecode("Domino";Request_Content)|,cdoc)  



代码中的Request_Content ,在帮助中无法查到,表示什么??公式执行的结果是获得提交上来的参数,但是不知道怎么解释这行代码。@URLDecode("Domino";Request_Content) 是对Request_Content转码,但是为什么要跟上cdoc当前文档。如果单独的queryAr=Request_Content  会报错,Request_Content即不是对象,也不是属性或方法。高手解释下把。

Request_Content  顾名思义

请求到参数
cdoc么 指的是当前文档的上下文(上下文是java的说法)

 

答2:

解决方法很简单,用encodeURI()方法,加一句话

var url="http://127.0.0.1:81/test/test.nsf/getsearchResult?openagent";
  function ajax_keyword(url) {
   xmlhttp = createXmlHttpRequest();
   xmlhttp.onreadystatechange = handleSearchSuggest;
url= encodeURI(url);
   xmlhttp.open("post",url, true);
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=GB2312");
   xmlhttp.send("keyword=" + keyword.value);
  }

原创粉丝点击