1. Htmlajax
$(document).ready(function(){
$("#bt").click(function(){
$.ajax({
type: "post",
dataType: "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
url: "SvJson",
data: {some:"some 文本"},
beforeSend: function(XMLHttpRequest){alert("before Send.");},
complete: function(){alert("complete")},
success: function(data){ alert("success"); alert("Some Thing: " + data.text);},
error: function(){alert("error");}
});
});
});json
设置contentType为防止中文上传乱码状况。app
dataType 设置返回数据类型。post
2. Javaurl
String text = request.getParameter("some");code
request.setCharacterEncoding("utf-8");
response.setContentType("text/json; charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write("{\"text\":\"success 终于\"}");
writer.flush();
writer.close();orm
符合json格式的字符串便可,不必定要是json对象。对象
PrintWriter 字符流需关闭。utf-8