jQuery AJAX 请求,后台收到为乱码
可以显式设置contentType的编码为utf-8, 后台一般都能正常解码例如:
[javascript] view plaincopy
contentType: "application/x-www-form-urlencoded; charset=utf-8",
[javascript] view plaincopy
jQuery(form).ajaxSubmit({
url: "doLogin?user=a",
type: "post",
dataType: "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success: showLoginResponse
});
如果不加contentType: "application/x-www-form-urlencoded; charset=UTF-8",而使用escape(),则会出现
post 主体包含的字节比内容长度指定的字节要少
的错误.
2.jQuery AJAX 返回乱码
你是否发现无论你怎么设置编码, 客户端收到的数据总是乱码呢, 请看下面的例子,你发现有什么问题吗?
[java] view plaincopy
void saveTempKind(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
resp.setContentType("text/text;charset=GBK");
try {
//DO PROCESS
pw.write("成功");
}catch(Exception e){
pw.write("失败");
}
}
问题在于resp.setContentType("text/text;charset=GBK"); 语句应该在
[javascript] view plaincopy
PrintWriter pw = resp.getWriter();
之前,而不是之后。如果象上例这样,无论你怎么设置response的编码,前端乱码的问题都依然如故。 可能的原因是pw在获得PriintWriter 之前,编码就已经确定了。所以编码的声明应该在获取PrintWriter对象之前http://blog.csdn.net/hongweigg/article/details/6756278
eb.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
我服务端
namespace ApplicationModuleDemo.Service
{
///
/// test 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class test : System.Web.Services.WebService
{
[WebMethod]
public IList GetList(int id)
{
IList list = new OrganizationInstitutionComments().GetBySQL("[institutionId]=" + id + " and [passed]=1");
foreach (OrganizationInstitutionCommentsInfo model in list)
{
model.UserPhoto = model.UserPhoto.Replace("./", AppConfig.API_SERVER + "/");
model.UserUrl = AppConfig.API_SERVER + "/BlogManage/UserInfo.aspx?pkId=" + model.UserId;
model.Content = new PageBase().InitContent(model.Content);
}
return list;
}
}
}
客户端、
$.ajax({
type: "POST",
contentType: "application/json",
data: { id: id },
dataType: 'json',
url: "../Service/test.asmx/GetList", success: function(msg) {
alert(msg);
}
});
.NET技术ASP.NET 李.net的主页 李.net | 老鸟四级 | 园豆:2643
提问于:2009-05-16 21:12
写“听云”原创博文,赢取iPhone 6超级大奖 收藏
最佳答案
1
data的写法不对,要写成
data: '{ id: ' + id + ' }'
注意data必须是一个“表示JSON对象的字符串”,而不是一个“JSON”对象
原因是jquery会把JSON对象序列化成标准POST的形式,你此处的{ id: id }会变成形好id=3这样的形式,而ASP.NET WebService需要的是JSON格式的数据,所以必须把你的数据变成一个JSON样子的字符串. jQuery AJAX 请求,后台收到为乱码
可以显式设置contentType的编码为utf-8, 后台一般都能正常解码例如:
[javascript] view plaincopy
contentType: "application/x-www-form-urlencoded; charset=utf-8",
[javascript] view plaincopy
jQuery(form).ajaxSubmit({
url: "doLogin?user=a",
type: "post",
dataType: "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success: showLoginResponse
});
如果不加contentType: "application/x-www-form-urlencoded; charset=UTF-8",而使用escape(),则会出现
post 主体包含的字节比内容长度指定的字节要少
的错误.
2.jQuery AJAX 返回乱码
你是否发现无论你怎么设置编码, 客户端收到的数据总是乱码呢, 请看下面的例子,你发现有什么问题吗?
[java] view plaincopy
void saveTempKind(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
resp.setContentType("text/text;charset=GBK");
try {
//DO PROCESS
pw.write("成功");
}catch(Exception e){
pw.write("失败");
}
}
问题在于resp.setContentType("text/text;charset=GBK"); 语句应该在
[javascript] view plaincopy
PrintWriter pw = resp.getWriter();
之前,而不是之后。如果象上例这样,无论你怎么设置response的编码,前端乱码的问题都依然如故。 可能的原因是pw在获得PriintWriter 之前,编码就已经确定了。所以编码的声明应该在获取PrintWriter对象之前http://blog.csdn.net/hongweigg/a ... 304201121393145234/
|