【问题标题】:Returning string as json in extJs在 extJs 中以 json 形式返回字符串
【发布时间】:2015-04-02 22:46:27
【问题描述】:

我正在开发 extJs 3.2.x

我有一个返回字符串的简单函数。

public Object getRevenueCurrency(....) {
    return "USD";
} 

我有 Jackson 映射器将响应类型映射到 JSON。

<bean name="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <property name="contentType">
            <!-- <value>application/json</value> -->
            <value>text/html</value>
        </property>
    </bean> 

尝试检索数据如下:

currencyStore = new Ext.data.JsonStore({
id:'currencystore',
url: 'xxxxxxx?action=getcurrency',
root: 'string',         
listeners: {load: function(store) {
       rev_currency=store.?????;
    }
}
});
currencyStore.on('exception',function( store, records, options ){
alert('Exception was called');
},this);

Fiddler 将来自服务器的响应显示为:

{"string": "USD"}

虽然我没有收到服务器或 js 异常,但调用了异常警报。

1.如何提取币值?
2.在上面的异常处理程序中,有什么方法可以提取关于异常的有意义的信息?

【问题讨论】:

    标签: extjs extjs4 extjs3


    【解决方案1】:

    您已经通过设置 root 属性来配置存储,这意味着存储的阅读器期望 json 中的根节点匹配“字符串”。

    所以读者实际上是在期待以下形式的回应。

    {"string":[{"propertyname":"USD"}]}
    

    相反,您可以只取出存储配置中的根属性,因为返回的只是一个平面对象

    【讨论】:

    • 取出根属性也不能阻止异常。我如何找出异常是什么?
    【解决方案2】:

    我注意到您尚未在商店中配置任何字段,因此当读者接受您的 json 响应时,它会尝试找到一个名为“字符串”的字段来绑定。

    查看 JsonStore 的 ExtJS3 文档 -> http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.data.JsonStore

    示例配置向您展示了字段的定义方式,因此您的商店应如下所示:

    currencyStore = new Ext.data.JsonStore({
    id:'currencystore',
    url: 'xxxxxxx?action=getcurrency',
    fields: ['string'],         
    listeners: {load: function(store, records, options) {
           rev_currency=store.?????;
        }
    }
    });
    

    注意我也将参数更改为您的加载函数处理程序,因此可以使用 records[0] 访问从您的 json 创建的新记录

    【讨论】:

      【解决方案3】:

      关于你的第二个问题:

      2.在上面的异常处理程序中,有什么方法可以提取关于异常的有意义的信息?

      根据docs for the exception event的签名和参数是:

      exception( store, type, action, options, response, arg )
      

      通过检查参数(例如通过 console.log(...)),您应该检索到一些有用的信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        • 2023-04-11
        • 1970-01-01
        相关资源
        最近更新 更多