*在我们的代码中,Ext JS页面中的参数被传递给我们拥有业务逻辑的Spring 3控制器。然后在控制器中,使用getWriter.write设置响应对象,并在Ext JS页面中检索响应。 问题:在解码响应时,Firebug在使用Ext.util.JSON.decode时显示错误,因此我们不得不使用Ext.decode来解码来自服务器的响应。但是Ext.decode给出了一个值:object Object。我需要将其转换为String或格式。 控制器代码:
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping
public class SampleController {
@RequestMapping(value = "/login.htm",method = RequestMethod.POST)
@ResponseBody
public void validateCredentials(@RequestParam("user") String user,
@RequestParam("password") String password,HttpServletResponse response) {
boolean flag = false;
String resultString = null;
try {
response.setContentType("application/json");
response.setHeader("Content-Type", "text/html, charset=utf-8");
if (user.equals(password)) {
flag = true;
resultString = "{success:true}";
} else {
flag = false;
resultString = "{success:false}";
}
response.getWriter().write(resultString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Ext JS Login form :
Ext.onReady(function(){
function submit(button,event){
var uname=Ext.getCmp('user').getValue();
alert("1"+uname);
var passWord=Ext.getCmp('password').getValue();
Ext.Ajax.request({
url: 'login.htm',
method :'POST',
params: {
user:uname,
password:passWord
},
success: function(result,request) {
var jresp = Ext.JSON.decode(result.responseText);
//Ext.JSON.decode stores object Object in jresp. Our requirement is converting jresp to String or boolean
console.log('Success'+jresp);
},
failure: function(response, request) {
var jresp = Ext.JSON.decode(result.responseText);
console.log(jresp.error);
console.log('server-side failure with status code 8 ' + response.status);
}
});
}
var myform = new Ext.form.FormPanel({
title:'Login form',
frame:true,
width:400,
height: 250,
url:'login.htm',
method:'POST',
renderTo:'div1',
items:[
{
xtype:'textfield',
fieldLabel:'user',
inputType:'user',
allowBlank:false,
minLengthText:'3',
id:'user',
name:'user'
},
{
xtype:'textfield',
fieldLabel:'password',
inputType:'password',
allowBlank:false,
minLengthText:'3',
id:'password',
name:'password'
}
],
buttonAlign:'center',
buttons:[
{
text:'reset',handler:function(){myform.getForm().reset();}
},
{
text:'Login',
handler: submit
}
]
});
});
答案 0 :(得分:0)
在解码响应时,Firebug在使用时显示错误 Ext.util.JSON.decode所以我们不得不使用Ext.decode来解码我们的 来自服务器的响应。
你得到错误,因为你在ExtJS4中使用了错误的对象,它的Ext.JSON.decode对数据进行编码而不是Ext.util.JSON.decode,你有这种混乱,因为ExtJS3有Ext.util.JSON.decode解析JSON字符串,但是故障安全的方法是使用Ext.decode,这是ExtJS3和ExtJS4中正确功能的简写
但是Ext.decode给出了一个值:object Object。我需要把它转换成 字符串或格式
这是正确的,因为您正在生成像"{success:true}"
这样的响应,因此当您解析该JSON字符串时,您将获得类似这样的内容
data = {'success':true};
因此您可以使用data.success
所以你的最终代码看起来像这样
var jresp = Ext.JSON.decode(result.responseText);
console.log('Success'+jresp.success);
使用此字符串
在spring mvc中生成响应还有一件事resultString =“{success:true}”;
这不是一个有效的json字符串,所以你需要将对象的键用双引号括起来像
resultString =“{\”success \“:true}”;
答案 1 :(得分:0)
为什么不传递一个Model实例,包括你在javascript(ExtJs)中需要的所有字段,你可以使用jackson转换你的模型做JSON。
只需在appContext中声明您的转换器:
包括杰克逊罐。
您还可以将成功和消息放入模型中。为简单起见,使用带有成功和消息字段的抽象ExtJsModel,并使所有模型扩展到相同的模型。
答案 2 :(得分:0)
您可以将这样的响应发送到extjs。然后使用extjs解码器。 这是代码。
var jresp = Ext.util.JSON.decode(result.responseText); 的console.log(jresp.success);
“” “{成功:真}” “”
如果你像这样控制。 的console.log( '成功' + jresp.success);
java-script总是给对象。所以不要使用它。