jQWidgets Forums

jQuery UI Widgets Forums Angular dataAdapter interceptor

This topic contains 3 replies, has 3 voices, and was last updated by  Martin 7 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • dataAdapter interceptor #100509

    silvano.alda
    Participant

    Hello,
    I am trying to intercept alla dataAdapter call to server, to inject jwt token in header, and also to intercept some response header I need.

    in angularjs I can do in this way:

    $(document).ajaxSend(function (event, jqxhr, settings) {
        if ($localStorage.token)
        {
          jqxhr.setRequestHeader("Authorization", 'Bearer ' + $localStorage.token);
        }
        $.xhrPool.push(jqxhr);
      });

    How can I do now in Angular 6? are component still using jquery to perform dataAdapter call?

    Thanks!

    dataAdapter interceptor #100518

    Martin
    Participant

    Hello silvano.alda,

    In Angular 6, I recommend you to use the HttpClient and HttpHeaders classes.
    You can easily add new requestHeaders and pass them in the request.
    You can read more about this in Angular’s Tutorial and Documentation.
    You can also check this Article for intercepting http in Angular 5.

    Best Regards,
    Martin

    jQWidgets Team
    http://www.jqwidgets.com/

    dataAdapter interceptor #100521

    codingbadger
    Participant

    Hi Martin – does the DataAdapter use HttpClient under the hood then?

    e.g. using this:

    const source: any = {
            datatype: 'json',
            datafields: [
              { name: 'CustomerId'},
              { name: 'FirstName'},
              { name: 'LastName'},
              { name: 'Email'}
            ],
            url: 'assets/data/some-data.json'
        };
    
       const dataAdapter: any = new jqx.dataAdapter(source);

    So writing a standard Http Client Interceptor in Angular would also intercept this url call?

    dataAdapter interceptor #100530

    Martin
    Participant

    Hello codingbadger,

    Sorry, I did not get your question completely the first time.
    You can set the request headers in the ‘beforeSend’ callback and then have access to the response xhr in the downloadComplete callback:

    const dataAdapter: any = new jqx.dataAdapter(this.source, {
            beforeSend: function (jqxhr, settings) { 
               jqxhr.setRequestHeader("Authorization", '124234234'); 
            },
            downloadComplete: function (data, status, xhr) { 
               console.log(xhr); 
            }
        });

    There are also other callback function that you can pass to the dataAdapter. You can check them in the Documentation.

    Best Regards,
    Martin

    jQWidgets Team
    http://www.jqwidgets.com/

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.