sergiodlopes / jquery-flexdatalist

Flexible input autocomplete/datalist plugin for jQuery
http://projects.sergiodinislopes.pt/flexdatalist/
MIT License
365 stars 84 forks source link

Header for authorization :'( #232

Closed bootexe closed 2 years ago

bootexe commented 2 years ago

Hi thanks for your library ! its really useful but I have a question dont know if its me but I can't add headers to the Ajax request I am sure its a dumb thing I havent seen but all seems logic to me right know

$('#shootingCtrl #adduser .flexdatalist').flexdatalist({
        minLength: 1,
        textProperty: '{firstName} {lastName}',
        valueProperty: 'mail',
        selectionRequired: true,
        visibleProperties: ["lastName","firstName"],
        searchIn: ['lastName','firstName','mail','phone'],
        data: endpoint+'/user/list',
        noResultsText:lang.error.NO_RESULT,
        params:{
            type:'GET',
            url : endpoint+'/user/list',
            crossDomain: true,
            headers:{
                "Authorization":_authtoken
            },
            beforeSend:function(xhr){
                xhr.withCredentials = true;
                xhr.setRequestHeader("Authorization",_authtoken); 
            }
        }
   });

this code is executed after DOMLoadedContent and the server is a sub domain api.code.com of the one hosting the website code.com but CORS has been enabled on both side and everything is working fine in other part with fetch like this one

function(cs=dcs,cf=dcf){
            fetch(endpoint+'/user/list',{
                method:'GET',
                mode:'cors',
                headers:{
                    'Authorization':authtoken
                }
            })
            .then(resp => resp.json())

Can u please help me iam lost The request is sended without the custom header

bootexe commented 2 years ago

I have corrected the problem by passing the data and initializing the flexdatalist on the result of the fetch

fetch(endpoint+'/user/list',{
        method:'GET',
        mode:'cors',
        headers:{
            'Authorization':authtoken
        }
    })
    .then(resp => resp.json())
    .then(response => {
            $('#shootingCtrl #adduser .flexdatalist').flexdatalist({
                    minLength: 1,
                    textProperty: '{firstName} {lastName}',
                    valueProperty: 'mail',
                    selectionRequired: true,
                    visibleProperties: ["lastName","firstName"],
                    searchIn: ['lastName','firstName','mail','phone'],
                    data: response,
                    noResultsText:lang.error.NO_RESULT
                })
     })