【问题标题】:how to get the server response.responseText after store load extjs 4存储加载extjs 4后如何获取服务器response.responseText
【发布时间】:2012-04-20 06:24:13
【问题描述】:

我在从 extjs 4 中的服务器响应中获取 response.responseText 时遇到了一个问题。

下面是我加载商店的代码:

store.load({
    params: {
        'projectid': this.projectid
    },
    callback: function (records, operation, success, response) {
        console.log(records);
        console.log(response.responseText);
    }
});

实际上,当我使用以下函数发出请求时,我正确地得到了reponse.responseText

Ext.Ajax.request({
    url: 'login/GetLoginCheck.action',
    method: 'GET',
    params: {
        'username': values['username'],
        'password': values['password']
        },
    scope: this,
    success: function(response) {
        Ext.Msg.alert(response.responseText);
        var redirect = response.responseText;
        window.location.href = "" + redirect + ".jsp";
    },
    failure: function(response) {
        Ext.Msg.alert('INVALID USERNAME OR PASSWORD');
    }
});

所以请建议我如何从具有回调函数的 store.load() 中获取 response.responseText

【问题讨论】:

    标签: extjs4 extjs-mvc


    【解决方案1】:

    回调有 3 个参数... 试试这个:

    store.load({
        params: {
            'projectid': this.projectid
        },
        callback: function (records, operation, success) {
            console.log(operation.response.responseText);
        }
    });
    

    【讨论】:

    • 服务器返回 500 时我没有“operation.response”。当服务器响应状态为 500 的错误消息时,我必须使用 Ext.Ajax.request 来显示消息和堆栈跟踪。跨度>
    【解决方案2】:

    我在使用 Model.load(...) 时遇到过类似的问题,但在我的情况下,operation.response 没有定义。所以,我找到了另一种获得它的方法:

    Model.load(1, {
        success: function () {
            // I haven't tested inside this callback yet
        },
        failure: function (record, operation) {
            var response = operation.request.proxy.reader.rawData;
            alert(response.message);
        }
    });
    

    【讨论】:

      【解决方案3】:

      你也可以试试这个..

      Ext.create('Ext.data.Store',{
          fields[],
          proxy:{url:'store_url.json', reader:{type:'json',root:'data'}},
          autoLoad:true,
          listeners:{
              load:function(store, record, success, opts){
                  var response_text = store.proxy.reader.rawData;
                  console.log(response_text);
              }
          }
      })
      

      【讨论】:

      • 这就是我要找的。如果success == falsestore.loadcallback 函数中的response 参数将是未定义的
      【解决方案4】:

      在 extjs 3.4 中你可以使用这个:

      this.historyInvoiceHeaderGrid.store.load({
              params:{start:0, limit:20},
              callback: function (records, operation, success) {
                  console.log(this.reader.jsonData);
              }});
      

      此属性store.reader.jsonData 将返回完整响应。

      也许对某些人来说它在 extjs 3 中会很有用。

      【讨论】:

        【解决方案5】:

        您必须在您的'Ext.data.Store' 中设置messageProperty 中的proxy reader

                    reader: {
                        type: 'json',
                        root: 'myDataList',
                        totalProperty: 'myTotalRecord',
                        successProperty: 'mySuccess',
                        messageProperty : 'myMsg'
                    }
        

        mySuccess 返回false 然后调用callback: function

            store.load({
                params: {start: 0, limit: 15},
                callback: function (records, operation, success) {
                    if (!success) {
                        try {
                            Ext.Msg.alert('Sorry !', operation.getError());
                            // operation.getError() returns myMsg value
                        }catch (e){
                            Ext.Msg.alert('Exception !', e);
                        }
                    }
                }
            });
        

        这是从 Java Servlet 返回的 json。

            Map<String, Object> myDataMap = new HashMap<>(3);
            try {
                // Something
                myDataMap.put("mySuccess", true);
                myDataMap.put("myMsg", "Whats up khomeni !");
            } catch (Exception e) {
                myDataMap.put("mySuccess", false);
                myDataMap.put("myMsg", "Whats wrong with me.");
            }
            String json = new Gson().toJson(myDataMap);
        

        【讨论】:

          【解决方案6】:

          在 Extjs 4.x 中它是这样工作的

          myStore.load({
                  url: 'myurl',
                  method: 'GET',
                  callback: function(records, operation, success) {
                  var jsonStr = Ext.JSON.decode(operation.response.responseText);
                      alert(jsonStr.message);
                   }
          });
          

          在 Extjs 5 中你必须这样做

          myStore.load({
                  url: 'myurl',
                  method: 'GET',
                  callback: function(records, operation, success) {
           var message=forecastMethodStore.getProxy().getReader().rawData.message;
                   }
          });
          

          但是这里的关键是你应该在java端的JSON响应中设置消息。

          示例:{"Root":[], "message":"duplicates"}"

          希望这会对某人有所帮助。

          【讨论】:

          • 使用 ExtJs 4.1.3 我对操作没有反应。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-13
          • 2011-01-21
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-29
          相关资源
          最近更新 更多