Title: easyui forms and ajax beforeSend
Post by: Juan Antonio Martínez on December 22, 2014, 05:23:33 AM
Hi all: I'm trying to use an special header for send SessionKey to server, so I added following code at body.onload(): // make sure that every ajax call provides sessionKey $.ajaxSetup({ beforeSend: function(jqXHR,settings) { if ( typeof(authInfo.SessionKey)!=undefined && authInfo.SessionKey!=null) { jqXHR.setRequestHeader('X-AC-SessionKey',authInfo.SessionKey); } else { // no SessionKey found, do not send special header alert("No Session Key"); // just for debug } return true; } });
This code works fine in almost $.ajax calls... but beforeSend is never invoked when I use easyui form('submit',options}. /** * Ask server routines for add/edit a prueba into BBDD */ function savePrueba() { // take care on bool-to-int translation from checkboxes to database $('#pruebas-Cerrada').val( $('#pruebas-Cerrada').is(':checked')?'1':'0'); // do normal submit $('#pruebas-form').form('submit',{ url: '/agility/server/database/pruebaFunctions.php', method: 'post', ajax: true, onSubmit: function(param){ return $(this).form('validate'); }, success: function(res){ var result = eval('('+res+')'); if (result.errorMsg){ $.messager.show({width:300, height:200, title:'Error',msg: result.errorMsg }); } else { $('#pruebas-dialog').dialog('close'); // close the dialog $('#pruebas-datagrid').datagrid('reload'); // reload the prueba data } } }); }
Seems that form submit is not performed by mean of ajax call... but easyui doc says that ajax call is used by default since 1.4 version... adding ajax:true to options doesn't affect result So what am I doing wrong, ¿How can I generate this request header on _every_ server call? Thanks in advance Juan Antonio
Title: Re: easyui forms and ajax beforeSend
Post by: Juan Antonio Martínez on December 22, 2014, 03:32:34 PM
This code works for me. It replaces $('#frm').form('submit,{...}); with an $.ajax equivalent Notice I had to move form('validate') outside of ajax call, to avoid override of beforeSend() method So seems to me that handling of 'ajax' property in jeasyui form is not working properly. ¿any ideas? function savePrueba() { // take care on bool-to-int translation from checkboxes to database $('#pruebas-Cerrada').val( $('#pruebas-Cerrada').is(':checked')?'1':'0'); var frm = $('#pruebas-form'); if (!frm.form('validate')) return; // don't call inside ajax to avoid override global beforeSend() $.ajax({ type: 'GET', url: '/agility/server/database/pruebaFunctions.php', data: frm.serialize(), dataType: 'json', // beforeSend: function(jqXHR,settings){ return frm.form('validate'); }, success: function (result) { if (result.errorMsg){ $.messager.show({width:300, height:200, title:'Error',msg: result.errorMsg }); } else { $('#pruebas-dialog').dialog('close'); // close the dialog $('#pruebas-datagrid').datagrid('reload'); // reload the prueba data } } }); }
Title: Re: easyui forms and ajax beforeSend
Post by: stworthy on December 22, 2014, 05:14:53 PM
The form plugin use a hidden iframe to submit form data. The beforeSend does not get called but you can use the 'onSubmit' callback function to do something before submitting the form data.
Title: Re: easyui forms and ajax beforeSend
Post by: bbones on August 17, 2015, 05:20:21 AM
Is there any change in this situation last year? I can't find good place to put something like xhr.setRequestHeader('X-AUTH-TOKEN', token); into form definition, only skip all library form component functionality. Pls help - it is very important for us now! BR
|