前台数据{
id:'user_update',
text: '编辑',
maxWidth:55,
handler:function(){
var user_form = this.up('form');
if(user_form.getForm().isValid()){
user_form.getForm().submit({
// url: 'updateUserServlet',
url:'MyJsp.jsp',
method:'post',
submitEmptyText: false,
waitTitle:'请等待',
waitMsg: '正在编辑用户...',
params : {
id : userid,
userName : userName,
userPermiss : userPermiss,
description : description,
userPasswd : userPasswd
},
success:function(form,action){
var response = Ext.decode(action.response.responseText);
Ext.Msg.alert('提示', response.msg);
userStore.load();
},
failure:function(form,action){
Ext.Msg.alert('提示', '编辑用户失败!');
}
});
}else{
Ext.Msg.alert('提示', '数据验证失败!');
}
}
}
后台代码:<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="net.sf.json.*" %>
<%
BufferedReader in=request.getReader();
StringBuffer jsonStr=new StringBuffer();
String str="";
while((str=in.readLine())!=null){
jsonStr.append(str);
}
JSONObject jsonObj= JSONObject.fromObject(jsonStr.toString());
String name= jsonObj.getString("userName");
String msg="";
out.print("asdf");
out.print(name);
%>
服务器报错为:严重: Servlet.service() for servlet jsp threw exception
net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of id=3&userName=yy&userPermiss=3&description=&userPasswd=usd1111
经过打印后,得到的数据为id=3&userName=yy&userPermiss=3&description=&userPasswd=usd1111不是json格式,请问哪里有问题??
用最简单的方式啊,你的代码太复杂了
[code="java"]
Enumeration em=request.getParameterNames();
String pm="";
JSONObject jsonObj = new JSONObject();
while(em.hasMoreElements()){
pm = em.nextElement();
jsonObj.put(pm, request.getParameter(pm));
}
String name= jsonObj.getString("userName");
[/code]
错误不是很明显么,json格式的数据必须包含{}
把这里改下试试
[code="java"]
StringBuffer jsonStr=new StringBuffer("{");
String str="";
while((str=in.readLine())!=null){
jsonStr.append(str);
}
jsonStr.append("}");
[/code]
肯定不会是JSON格式的啊 ,甚至你给出的那段代码里根本就没有JSON的影子.
[code="java"]
BufferedReader in=request.getReader();
StringBuffer jsonStr=new StringBuffer();
String str="";
while((str=in.readLine())!=null){
jsonStr.append(str);
}
[/code]
jsonStr 输出为id=3&userName=yy&userPermiss=3&description=&userPasswd=usd1111 也很正常.
如果你要使用json的话就需要手动构造出来(有些框架是有在后台帮你构造后,传到后台的.)
使用getParameter将参数一个一个取出来再构造出jsonObject, 不过就你这个例子直接用getParameter就可以了,再构造JsonObject就有点多此一举了
params: {
form.getForm().getValues() //取出表单所有值
},
Form提交一般都是 form的编码格式,而不会是json的
返回结果要求json罢了。
一般来说,post一个json反而比较麻烦。
因为http协议 定义的提交格式限定了,直接修改post数据体为json,要花点心思才能实现([b]不是说不能实现,只是 懒得研究了。[/b])。
用form的编码方式,tomcat会自动解码,然后就可以用request.getParameter("key")
去值了。
可以把json作为一个 form的参数的内容提交。
例如
params : {
content:Ext.encode(
{
id : userid,
userName : userName,
userPermiss : userPermiss,
description : description,
userPasswd : userPasswd
})
}