get the result of a function and send via post in another

0

I'm using the instascan lib to read qr code. but I am not able to send the result via post with ajax. Can someone help?

The code I'm using is below:

  qr sacanner      

<h1>qr scanner</h1>

<video id="preview"></video>
<div id="conteudo"></div>
<script type="text/javascript">
  let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
  scanner.addListener('scan', function lerQrcode(content) {
    //alert(content);
    gravar(content);
    document.getElementById('conteudo').innerHTML = content;

  });

  function gravar(){        
        $.ajax({
            method: "POST",
            url: "dados.php",
            data: '', // aqui deveria executar o post dos dados lidos
            success: function(data){                                
                alert(data.teste);
            }
        });
}


  Instascan.Camera.getCameras().then(function (cameras) {
    if (cameras.length > 0) {
      scanner.start(cameras[0]);
    } else {
      console.error('No cameras found.');
    }
  }).catch(function (e) {
    console.error(e);
  });
  // executar o post


</script>

    
asked by anonymous 29.11.2018 / 15:48

1 answer

0

You are calling the function gravar(content) by sending the value of content , then you must put the corresponding parameter in the function to receive that value, and play on the Ajax date:

function gravar(content){
   $.ajax({
      method: "POST",
      url: "dados.php",
      data: { codigo: content },
      success: function(data){                                
         alert(data.teste);
      }
   });
}

In the PHP page ( dados.php ) you get the value with $_POST['codigo'] .

    
29.11.2018 / 17:04