On the JavaScript side you can obtain the string representation of the document reference with:
const strDocRef = XWiki.Model.serialize(xm.documentReference);
See https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/FrontendResources/JavaScriptAPI/#HWorkwithEntityReferences . You can then pass this string to the server side (e.g. as a request parameter / query string parameter), where you resolve the reference:
#set ($queryingDocumentReference = $services.model.resolveDocument($request.documentReference))
Hope this helps,
Marius