2013-04-23 51 views
4

真的,我有一個form,其提交的是這樣的:ExtJS的4 Form.submit()不sucess:在回答

form.submit({ 
       url: '/postme', 
       waitMsg: 'Loading...', 
       method: 'POST', 
       success: function (form, action) { 
        console.log(action.response.responseText);         
       } 
      }); 

但是!主要的是,在回答我沒有財產success: true,並無法管理添加它。有沒有辦法使callback參數在Ext.Ajax.request 而不是success?或者也許有辦法告訴form.submit()它必須檢測其他響應參數中的成功?

順便說一句:a不能使用Ext.Ajax.request,因爲我必須從窗體(文件上傳)傳遞多部分數據。

在此先感謝。

回答

9

API解釋說您必須通過success: true才能調用成功處理程序。這是submit方法該文檔

@param {對象}選項 的選項傳遞給動作(見Ext.form.Basic.submit和Ext.form.Basic.doAction的詳細信息)

if (form.isValid()) { 
    // Submit the Ajax request and handle the response 
    form.submit({ 
     success: function(form, action) { 
      Ext.Msg.alert('Success', action.result.message); 
     }, 

     // If you don't pass success:true, it will always go here 
     failure: function(form, action) { 
      Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response'); 
     } 
    }); 
} 

如果要使用不同的屬性來指示錯誤,你應該看看http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.form.Basic-cfg-errorReader,您可以配置errorReader閱讀你的財產,你想。

您可以路由你都成功和失敗處理到同一個地方,做從處理程序確定它

一定要設置standardSubmit:true所以它不會與AJAX提交

樣品溶液這將使你的代碼感覺有點不太髒

Ext.define('ns.my.CustomReader', { 
    extend: 'Ext.data.reader.Reader', 
    alias: 'reader.ns-customreader', 
    read: function(xhr) 
     var resp = Ext.JSON.decode(response.responseText, true); 
     // Handle the case where response is not JSON too (unhandled server errror?) 
     return {success: resp ? !!resp.id : false}; 
    } 
}); 

然後,當您創建表單,你可以只使用

form : { 
    errorReader: 'ns-customreader' 
} 

我也注意到,你沒有返回記錄,讀者應該根據文檔來做,它可能是你不需要它,但它會很高興滿足界面以使您的代碼符合Ext-JS

errorReader並不一定是一個完整的Reader實現。它只是需要實現一個讀(XHR)函數,它具有以下結構返回記錄的數組中的對象:

{ records: recordArray } // I think it needs success: boolean also. 
+0

有什麼意義?我期待API,並沒有找到解決方案來告訴,即使從服務器回答沒有參數「成功:真」,響應可以是成功的。正如我所說我需要在[Ext.Ajax.request](http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.Ajax-method-request)中的'callback'參數 – 2013-04-24 13:24:43

+0

@AndrewKevich如果你不通過'success:true',它會被路由到失敗端口,只是在那裏處理?你不能在你的迴應中加入'success:boolean'? – 2013-04-24 13:59:22

+0

@AndrewKevich如果你真的想要另一個參數來確定它是否是一個錯誤,你需要傳遞'Ext.data.reader.Reader'作爲'errorReader'配置http://docs.sencha.com/extjs /4.2.0/#!/api/Ext.form.Basic-cfg-errorReader – 2013-04-24 14:06:23

0

如果你不發送success: true它顯然是失敗的。因此,處理您的參數與此類似故障的回調函數,你的替代successstatus

form.submit({ 
    url: '/postme', 
    waitMsg: 'Loading...', 
    method: 'POST', 
    success: function (form, action) { 
     console.log(action.response.responseText);         
    }, 
    failure: function(form, action) { 
     if (action.result.status == true) { 
      console.log('success!'); 
     } 
    } 
}); 
0

除了@Juan門德斯回答我發現了以下情況必須是真實的:

在服務器端

  • 正確的JSON需要有一個成功真正:{ 「成功」:真正}
  • 返回的contentType必須設置爲text/html的

這對版本的ExtJS-3.4.0是真的

請看:Why success callback is not called in extjs form submission?

這爲我節省了很多時間我希望它能幫助別人。