Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
How to use fetch sent the Data URI from canvas to google sheet

I want to sent the image as base64 code to google sheet by using fetch,but when I run my code it will shows error "data_sent is not defined".

I want to know where is the problem and how to fix it.

*image is the variable to save the image builded by canvas, image_data is the variable to save the base64 from image

this.saveAsImg = function() {
    var image = new Image();
    image.src = this.canvas.toDataURL("image/png");
    if (image.src == this.emptyCanvas) {
        alert('請先書寫')
    } else {
        console.log('提交的內容===>', image.src)
    }
    return image.src;
};
var image_data;

document.getElementById('save').onclick = function() {
    image_data = writeCanvas.saveAsImg();
};

function post_trigger(image_data, uuid) {
    let url = 'localhost:5050';
    let data_sent = image_data;
    fetch(url, {
        method: 'POST',
        
        headers: {
            'Content-Type': 'application/json'
        },
        
        body: JSON.stringify({
            email: data_sent
                //deviceID: uuid
        })
    }).then((response) => {
        return response.json();
    }).then((jsonData) => {
        console.log(jsonData);
    }).catch((err) => {
        console.log('錯誤:', err);
    })
}
document.getElementById('save').onclick = post_trigger(image, uuid);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I changed my code and here is the new code. It can connect to api and post Data URI successfully.

let post_trigger = document.getElementById('sent');

    post_trigger.addEventListener('click', async function() {
        let url = 'http://127.0.0.1:5000/test2';
        let formData = new FormData();
        formData.append("email", str);
        let result = await fetch(url, {
            method: 'post',
            body: JSON.stringify({
                uri: str
            }),
            headers: {
                'content-type': 'application/json'
            },
        }).then(res => {
            return res.text();
        })
        alert(result);
    })

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda