Bonsoir,

J'ai le code JS suivant pour envoyer une image à partie de mon emulateur/téléphone vers un serveur (en fait il va interagir avec un script php hébergé dans mon localhost):

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
document.querySelector('#afile').addEventListener('change', function(e) {
  var file = this.files[0];
  var fd = new FormData();
  fd.append("afile", file);
 
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'http://192.XXX.XXX.X/myproject/saveme.php', true);
 
  xhr.upload.onprogress = function(e) {
    if (e.lengthComputable) {
      var percentComplete = (e.loaded / e.total) * 100;
      console.log(percentComplete + '% uploaded');
    }
  };
  xhr.onload = function() {
   console.log("YES!!!");
   console.log(this.status+ " // " +this.response);
    if (this.status == 200) {
 
      var resp = JSON.parse(this.response);
      console.log('Server got:', resp);
      var image = document.createElement('img');
      image.src = resp.dataUrl;
      document.body.appendChild(image);
    };
  };
  xhr.send(fd);
}, false);
voici le code HTML correspondant:

Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 <input type="file" accept="image/*;capture=camera" id="afile" >
  </label>
		<p>Photo:</p>
		<canvas id="capturedPhoto" width="250" height="250">
 
</canvas>

Le problème lorsque j'exécute le code, je reçois l'affichage suivant:

file:///index.html (45) :80.09612687924704% uploaded
92.74288375491763% uploaded
100% uploaded
YES!!!
file:///index.html (1) :Uncaught SyntaxError: Unexpected token $ in JSON at position 0
file:///index.html (45) :100% uploaded
file:///index.html (49) :YES!!!
file:///index.html (1) :Uncaught SyntaxError: Unexpected token $ in JSON at position 0
La position 0 correspond à la ligne suivante:

<!DOCTYPE html>

Je me demande c'est quoi le problème?

Merci pour votre aide en avance!