2012-04-20 38 views
7

Tôi đang gặp một vấn đề với nhận được response.responseText từ phản ứng máy chủ trong ExtJS 4.làm thế nào để có được response.responseText máy chủ sau khi ExtJS cửa hàng tải 4

Dưới đây là mã của tôi để nạp các cửa hàng:

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

Thực ra, khi tôi thực hiện yêu cầu với chức năng dưới đây, tôi có được 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'); 
    } 
}); 

Vì vậy, vui lòng đề nghị tôi làm cách nào để có được response.responseText từ store.load() có chức năng gọi lại.

Trả lời

7

callback có 3 thông số ... thử điều này:

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

Tôi không có "operation.response" khi máy chủ trả về 500. Khi máy chủ phản hồi thông báo lỗi với trạng thái 500, tôi phải sử dụng Ext.Ajax.request để hiển thị thông báo và ngăn xếp. – df1

1

tôi đã phải đối mặt với một vấn đề tương tự sử dụng Model.load(...), nhưng trong trường hợp của tôi, operation.response không được định nghĩa. Vì vậy, tôi đã tìm thấy một cách khác để có được nó:

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); 
    } 
}); 
0

Trong ExtJS 3.4 bạn có thể sử dụng này:

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

này sở hữu store.reader.jsonData sẽ trả về phản hồi đầy đủ.

lẽ cho một người nào đó nó sẽ là hữu ích trong ExtJS 3.

1

Bạn cũng có thể thử loại này ..

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); 
     } 
    } 
}) 
+0

đây là những gì tôi đang tìm kiếm. tham số 'response' trong hàm' callback' của 'store.load' sẽ không được xác định nếu 'success == false' –

0

Trong ExtJS 4.x nó đang làm việc như thế này

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

Trong Extjs 5 bạn phải làm như thế này myStore.load({ url: 'myurl', method: 'GET', callback: function(records, operation, success) { var message=forecastMethodStore.getProxy().getReader().rawData.message; } });

Nhưng điểm mấu chốt ở đây là bạn nên đặt thông báo trong phản hồi JSON từ phía java. Mẫu: "{\" Gốc \ ": [], \" thư \ ": \" trùng lặp \ "}"

Hy vọng điều này sẽ giúp ích cho ai đó.

+0

Sử dụng ExtJs 4.1.3 và tôi không có phản hồi về thao tác. – cmart

0

Bạn phải đặt messageProperty trong proxy reader trong số 'Ext.data.Store'.

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

khi mySuccess lợi nhuận false sau đó gọi 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); 
       } 
      } 
     } 
    }); 

Đây là sự trở lại của json từ Java Servlet.

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); 
Các vấn đề liên quan