EasyUI Forum
May 15, 2024, 01:50:43 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: easyui forms and ajax beforeSend  (Read 13838 times)
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« 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():
Code:
	// 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}.
Code:
/**
 * 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
« Last Edit: December 22, 2014, 05:29:27 AM by Juan Antonio Martínez » Logged
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« Reply #1 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?

Code:
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
            }
        }
    });
}
« Last Edit: December 22, 2014, 04:59:05 PM by Juan Antonio Martínez » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #2 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.
Logged
bbones
Newbie
*
Posts: 11


View Profile Email
« Reply #3 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

Code:
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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!