A
A
Artem Kolchin2022-04-20 22:10:18
PHP
Artem Kolchin, 2022-04-20 22:10:18

How to make an ajax request in javascript?

Tell me, I'm writing a javascript script, I want to take a screenshot on the canvas (this will be a set of images that are formed when passing the questionnaire) and share it on a social network.
Total found this library
https://html2canvas.hertzen.com/

Further on js

window.onload = function() {
    document.getElementById("qq").onclick = function() {
        html2canvas(field).then(function(canvas) {
            var my_screen = canvas.toDataURL('image/jpeg', 0.5)
            console.log(my_screen)
          document.getElementById("vk").addEventListener('click', function(){
            window.open('https://vkontakte.ru/share.php?url='+my_screen+',sharer,status=0,toolbar=0,width=650,height=500')
        })
        });
    };
}


But since in my_screen, there is an image in base64, I want to resort to ajax and php

. That is, the algorithm is like this, I collect a screenshot on the canvas with a function, using ajax I save the image to the site folder and return the image url in the form of a link where it lies.

Well, then the script in order to share

window.open('https://vkontakte.ru/share.php?url='+my_screen+',sharer,status=0,toolbar=0,width=650,height=500')


Ajax figured out how it works on jquery , but on vanilla js I don’t understand how to do it.
In php I think like this

<?php
  $name = time().".jpg";
  $check_save = @file_put_contents($name, base64_decode($_POST["data"]));
?>


How correct was the decision formed in my head, and how to send an ajax request exactly on vanilla js?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2022-04-20
@html_student

To what extent the correct decision was formed in the head,
Pretty standard solution.
and how to send an ajax request exactly on vanilla js?
XMLHttpRequest or fetch, in fact, there is no difference, the only thing to take into account is that the data through the fetch will by default go to php://input , and not to $_POST, or you need to form a request with formData as the sent data structure.
Is it possible to solve the problem without a backend?
No. It is not clear where this conclusion comes from ...

A
A person from Kazakhstan, 2016-10-15
@LenovoId

look there are even examples

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question