how i send withCredentials property after upgrading v23

Hi, before upgrading to v23, i passed the withCredentials property inside the beforeSend function ​in a class  that extended ODataV4Adaptor:

Image_9527_1698156579790

however, after upgranding packages to v23, has an error inside the function

Image_2291_1698156843971

the message error is: 

Argument of type 'XMLHttpRequest' is not assignable to parameter of type 'Request'.

I was expecting Request, but you passed XMLHttpRequest.


And when I change the typing to Request, there is no property called withCredentials. So how can I pass this property after upgrading to v23?


3 Replies 1 reply marked as answer

JC Joseph Christ Nithin Issack Syncfusion Team November 3, 2023 10:41 AM UTC

Hi Rellyson



In our latest DataManager(v23), we are using fetch request instead of our default AJAX library (which uses XMLHttpRequest). For AJAX, we can enable cookies in the browser by setting "withCredentials" to true in the Datamanager’s beforeSend() method. For Fetch, we can enable cookies by setting “credentials” as include. However, when using fetch requests, we cannot directly set the “credentials” as “include” in the beforeSend() method.


Because the credentials property of a fetch request is initially set to read-only, so if you want to enable cookies, you can override the beforeSend() method and generate a new fetch request to set the credentials as include to achieve your requirement. Please refer the below code example for more for information.


 

export class DataManagerAdaptor extends OdataV4Adaptor {

public beforeSend (args, xhr, settings): void {

  //inside beforeSend here you have create a new Request and set credentials as include

var newFetchRequest = new Request(settings.url, {

        method: settings.fetchRequest.method,

        headers: { 'Syncfusion': true},

        credentials: 'include'// Set credentials to 'include' here

    });

    settings.fetchRequest = newFetchRequest;

 

}

}

 


Regards,

Joseph I.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Marked as answer

LK Lukas Kubaliak May 21, 2024 01:26 PM UTC

Hello,


 and how does it work for URLAdaptor that does not use Settings?


Lukas



JS Johnson Soundararajan S Syncfusion Team May 24, 2024 06:49 AM UTC

Hi Lukas Kubaliak,


Thank you for reaching out to us.


If you wish to include headers, you can do so directly. However, if you want to enable cookies, setting the "credentials" as "include" in the beforeSend() method is not possible.


To achieve this, you can override the beforeSend() method and create a new fetch request with the credentials set as include. Additionally, it is essential to specify the content format on your end.


Please refer to the below code snippet for more information.


Code sample :

 

      beforeSend (args, xhr, settings, request){

          var newFetchRequest = new Request(settings.url, {

              method: settings.fetchRequest.method,

              headers: { 'Syncfusion': true, 'Content-Type': '*'},

              credentials: 'include',

          });

      settings.fetchRequest = newFetchRequest;

      }

 


Please get back to us, if you need further assistance.


Regards,

Johnson Soundararajan S


Loader.
Up arrow icon