【问题标题】:Convert response from server (JSON object) to string in extjs将来自服务器(JSON 对象)的响应转换为 extjs 中的字符串
【发布时间】:2012-04-07 22:06:27
【问题描述】:

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

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
                            }

                            ]       

                   });






});

【问题讨论】:

  • 您能发布您收到的回复吗?如果是 JSON,你应该可以使用 JSON.Decode

标签: json spring-mvc extjs4


【解决方案1】:

您可以将这样的响应发送到 extjs 。然后使用 extjs 解码器。 这是代码。

var jresp = Ext.util.JSON.decode(result.responseText); console.log(jresp.success);

"""{success:true}"""

如果你这样安慰。 console.log('成功'+jresp.success);

java-script 总是给对象。所以不要使用这个。

【讨论】:

    【解决方案2】:

    为什么不在 javascript (ExtJs) 中传递一个包含您需要的所有字段的 Model 实例,您可以使用 jackson 将您的模型转换为 JSON。

    只需在 appContext 中声明你的转换器:

    包括杰克逊罐子。

    您也可以将成功和消息放入模型中。为简单起见,使用带有成功和消息字段的抽象 ExtJsModel,并使您的所有模型都扩展同一个模型。

    【讨论】:

      【解决方案3】:

      在解码响应时,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 字符串时,您会得到类似的结果

      数据 = {'成功' : true};

      因此您可以使用 data.success 获取布尔值

      所以您的最终代码将如下所示

      var jresp = Ext.JSON.decode(result.responseText); 
      console.log('Success'+jresp.success);
      

      你正在使用这个字符串在 spring mvc 中生成响应的另一件事

      resultString = "{success:true}";

      这不是一个有效的json字符串,所以你需要用双引号将对象的键括起来

      resultString = "{\"success\":true}";

      【讨论】:

        猜你喜欢
        • 2017-01-17
        • 2022-11-19
        • 2018-04-06
        • 2011-08-19
        • 2014-10-05
        • 1970-01-01
        • 2018-05-08
        • 2014-11-11
        • 2018-06-28
        相关资源
        最近更新 更多