extjs读取服务器信息,将响应从服务器(JSON对象)转换为extjs中的字符串

*在我们的代码中,来自Ext JS页面的参数被传递到Spring 3控制器,我们拥有业务逻辑。然后在控制器中,使用getWriter.write设置响应对象,并在Ext JS页面中检索响应。 问题:在解码响应时,Firebug在使用Ext.util.JSON.decode时显示错误,因此我们不得不使用Ext.decode来解码来自服务器的响应。但Ext.decode提供了一个值:object Object。我需要将其转换为字符串或格式。 控制器代码:将响应从服务器(JSON对象)转换为extjs中的字符串

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

你能发表你的回复吗?如果是JSON,你应该可以使用JSON.Decode –

2012-03-23 10:58:56

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值