0

Tengo una funcion que hace una peticion a un endpoint de una API, esta api arma un pdf y responde, el problema es que aparece un mensaje de error en la peticion ajax:

Este es el error que obtengo como respuesta.

Uncaught DOMException: XMLHttpRequest.responseText getter: responseText is only available if responseType is '' or 'text'.

El codigo es el siguiente:

function ImprimirApiExterno(url, elemento = null) {

ShowModalCargando(true);

$.ajax({
    cache: false,
    contentType: "application/json",
    type: "GET",
    url: url,
    processData: false,
    xhrFields: {
        responseType: "blob"
    },
    success: function (response, status, xhr) {
        console.log("Operacion exitosa")
        try {
            //ShowModalCargando(false);
            let blob = new Blob([response], { type: 'application/pdf' });
            let URL = window.URL || window.webkitURL;
            let downloadUrl = URL.createObjectURL(blob);

            if (elemento === null)
                window.open(downloadUrl);
            else {
                let iFrame = document.getElementById(elemento);
                iFrame.src = downloadUrl;
            }

        } catch (ex) {
            ShowModalCargando(false);
            console.log(ex);
        }
    },
    error: function (err) {
        ShowModalCargando(false);
        console.log("Error al intentar realizar la impresion: " + JSON.stringify(err));
    }
});

}

  • Ya intente omitir el responseType, y me entrega un pdf vacio
  • Intente usar una version de JQuery mas actual y no funciono (uso la 1.11.3)
  • Intente omitir el contentType, no funciono
  • Al parece ni siquiera pasa por el success
  • Ya probe el mozilla, en google chrome y el error persiste
  • Probando en postman, si funciona

1 respuesta 1

0

Finalmente pude realizar de otra forma, la hipotesis principal es la version de JQuery que se esta usando en el proyecto (un poco antigua) El codigo quedo de la siguient forma

                                                                         
      try {
         //Tuve que usar XMLHttpRequest en lugar de $.ajax
         var req = new XMLHttpRequest
         //Url del endpoint, metodo get 
         req.open('GET', url, true)
         req.responseType = 'blob'
         req.onload = function (res) {
             console.log(req.response)
             let blob = new Blob([req.response], { type: 'application/pdf' });
             let URL = window.URL || window.webkitURL;
             //Abrir el document en una nueva pestana
             let downloadUrl = URL.createObjectURL(blob);
             window.open(downloadUrl);
         }
         req.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
         //Agregar el token
         req.setRequestHeader('Authorization', sToken);
         //Enviar peticion
         req.send()
     } catch (err) {
         ShowModalCargando(false);
         console.log("Ocurrio un error al solicitar la impresion")
         console.error(err)
     }

Por si a alguien mas le sirve.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.