转自:http://blog.csdn.net/wangqiuyun/article/details/8450964

字符编码这个东西,一旦和中文打上交道就不可避免出现乱码,今天项目用到了JQuery向ashx提交中文参数的这一块,折腾了一天,开始是各种乱码,最后中算弄好了。

客户端:

  1. //异步获取数据
  2. var tpAction=action+'getSearch.ashx?key='+encodeURIComponent('中国');
  3. $.getJSON(tpAction,function(rsp){//请求成功
  4. if(rsp.status=='1'){
  5. var list=rsp.item;
  6. var len=list.length;
  7. if(len==0){
  8. //没有数据直接返回
  9. setNull();
  10. return;
  11. }
  12. //组装数据
  13. var s=zy_tmpl(templ,list,len);
  14. $list.append(s);
  15. }else{
  16. alert('','加载数据失败,请重试','确定');
  17. }
  18. },'json',function(err){//请求失败
  19. alert('','加载失败,请检查网络设置!','确定');
  20. },'POST','');
//异步获取数据
var tpAction=action+'getSearch.ashx?key='+encodeURIComponent('中国');
$.getJSON(tpAction,function(rsp){//请求成功
if(rsp.status=='1'){
var list=rsp.item;
var len=list.length;
if(len==0){
//没有数据直接返回
setNull();
return;
}
//组装数据
var s=zy_tmpl(templ,list,len);
$list.append(s);
}else{
alert('','加载数据失败,请重试','确定');
} },'json',function(err){//请求失败
alert('','加载失败,请检查网络设置!','确定');
},'POST','');

服务器端:

  1. string keyy = context.Request["key"];
  2. if (keyy != null)
  3. {
  4. string key = HttpUtility.UrlDecode(context.Request["key"].ToString());
  5. //string key = context.Server.HtmlDecode(context.Request.Params["key"].ToString());
  6. string log = "getSearch.ashx?key=" + key;
  7. string logsql = "insert into logs(createtime,contents) values('" + DateTime.Now.ToString() + "','" + log + "')";
  8. Sqlbase.ExecuteNonQuery(CommandType.Text, logsql, null);
  9. //more
  10. string sql = "select * from news where title like '%" + key + "%' order by id desc";
  11. DataTable dt = Sqlbase.ExecuteTable(CommandType.Text, sql, null);
  12. result rs = new result();
  13. rs.status = 1;
  14. rs.msg = "成功";
  15. rs.item = dt;
  16. string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(rs);
  17. context.Response.Clear();
  18. context.Response.ContentEncoding = Encoding.UTF8;
  19. context.Response.ContentType = "application/json";
  20. context.Response.Write(strJson);
  21. context.Response.Flush();
  22. context.Response.End();
  23. }
        string keyy = context.Request["key"];

        if (keyy != null)
{ string key = HttpUtility.UrlDecode(context.Request["key"].ToString());
//string key = context.Server.HtmlDecode(context.Request.Params["key"].ToString());
string log = "getSearch.ashx?key=" + key;
string logsql = "insert into logs(createtime,contents) values('" + DateTime.Now.ToString() + "','" + log + "')";
Sqlbase.ExecuteNonQuery(CommandType.Text, logsql, null); //more
string sql = "select * from news where title like '%" + key + "%' order by id desc";
DataTable dt = Sqlbase.ExecuteTable(CommandType.Text, sql, null);
result rs = new result();
rs.status = 1;
rs.msg = "成功";
rs.item = dt;
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(rs); context.Response.Clear();
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.ContentType = "application/json";
context.Response.Write(strJson);
context.Response.Flush();
context.Response.End();
}

这里根本就没有进行什么编码设置,其实就是JS用encodeURIComponent()对中文字符串编码,C#用HttpUtility.UrlDecode(context.Request["key"].ToString())进行解码,是不是很简单呢!可是我一天的实践证明:JS端用encodeURIComponent()要比escape()好多了,至于区别大家可以google一下,C#的解码函数也一堆,像什么Server.UrlDecode之类的,但是推荐使用HttpUtility.UrlDecode(),好处大家同样也可以google!

JQuery向ashx提交中文参数方案 [转]的更多相关文章

  1. JQuery向ashx提交中文参数方案

    客户端://异步获取数据var tpAction=action+'getSearch.ashx?key='+encodeURIComponent('中国'); $.getJSON(tpAction,f ...

  2. 解决ajax get post方式提交中文参数乱码问题

    最近在工作中遇到,使用ajax get方式提交中文参数的时候出现乱码,通过上网搜索,总结出比较简单的两种解决方案: 第一种,由于tomcat默认的字符集是ISO-8859-1,修改Tomcat中的se ...

  3. 解决ajax get方式提交中文参数乱码问题

    最近在工作中遇到,使用ajax get方式提交中文参数的时候出现乱码,通过上网搜索,总结出比较简单的两种解决方案: 第一种,由于tomcat默认的字符集是ISO-8859-1,修改Tomcat中的se ...

  4. Java Web开发总结(三) —— request接收表单提交中文参数乱码问题

    1.以POST方式提交表单中文参数的乱码问题 <%@ page language="java" import="java.util.*" pageEnco ...

  5. JS调用ashx文件传递中文参数取不到值的解决方案

    引自:http://www.cnblogs.com/yinpeng186/archive/2011/09/30/2196726.html

  6. jquery .post .get中文参数乱码解决方法详解

    jquery默认的编码为utf-8,做项目时有时处于项目需要用到ajax提交中文参数,乱码问题就很头疼了,折腾了许久终于弄出来了.为了便于传输,我们首先将需要用到的参数用javascript自带的函数 ...

  7. 用get方式提交请求的url带有中文参数

    又碰到JSP页面中文乱码问题,经过一次encodeURI处理后仍旧是乱码,后来经过两次encodeURI后正常显示中文 以前也碰到过同样的问题,没深究,这次网上搜集了一些资料,记录下来留做备份 ___ ...

  8. struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

    我的前台页是这样的: <body>      <form action="test.action" method="post">     ...

  9. request接收表单提交数据及其中文参数乱码问题

    一.request接收表单提交数据: getParameter(String)方法(常用) getParameterValues(String name)方法(常用) getParameterMap( ...

随机推荐

  1. iOS开发资料

    https://github.com/XCGit/awesome-objc-frameworks https://github.com/KevinHM/ios-good-practices-the-l ...

  2. 自行解决12306页面显示异常的问题(长城宽带下WWW。12306无法正常使用)

    前二天突然发现家里所用的长城宽带的www.12306.cn无法正常显示,点击余票查询或者车票预订均打不开,加载时间非常长,现象好似CSS等资源文件未载入成功(如图所示)更换chrome.firefox ...

  3. [OS] 可执行文件的装载

    http://www.jianshu.com/p/e1300e7a4c48 1. 虚拟内存 在早期的计算机中,程序是直接运行在物理内存上的,程序在运行时访问的地址就是物理地址.可是,当计算机中同时运行 ...

  4. RHEL7 本地yum源配置

    配置yum 源 1.挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别  [root@ mnt]# mount /dev/cdrom /mnt 2.在目录/etc/yum.r ...

  5. 2dsphere索引

    概念:球面地理位置索引 创建方式: db.collection.ensureIndex({w:'2dsphere'}) wdspere中,位置的表示方式不再是简单的经度,纬度,数组,而是变成一种复杂的 ...

  6. 设置meta标签 清除页面缓存,如:<meta http-equiv="Cache-Control" content="no-cache"/>

    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" ...

  7. C# sizeof运算符

    一.C# sizeof运算符 sizeof运算符用于获取值类型的字节数. 二.示例 using System;using System.Collections.Generic;using System ...

  8. Linux问题分析或解决_samba无法连接

    1. windows设置方面问题 问题:window能连接部分服务器的samba共享,一部分无法连接.报错如截图. 解决:前提---其他人连接都没有问题,发现有问题的连接服务器的电脑是win10,而w ...

  9. 认识/etc/passwd和/etc/shadow

    认识/etc/passwd和/etc/shadow============================== /etc/passwd [root@aminglinux ~]# head -n1 /e ...

  10. ASP.NET Core模块化前后端分离快速开发框架介绍之1、开篇

    源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...