2017-05-19 5 views
1

Meine Ajax-Anfrage legt Dateien in das Verzeichnis.
Ich bekomme jedoch keine Antwort von der PHP-Datei.AJAX empfängt keine PHP-Antwort

Ich verwende Warnungen, um festzustellen, ob eine Antwort eingegangen ist.

Jede Hilfe würde sehr geschätzt werden.

function doodleSave() { 
    var canvas = document.getElementById("doodle-canvas"); 
    var canvasData = canvas.toDataURL("image/png"); 
    $.ajax({ 
    url:'test.php', 
    type:'POST', 
    data:{ data:canvasData }, 
    success: function(response){ 
     alert(response); 
     //echo what the server sent back... 
    } 
    }); 
} 


<?php 

/* AUTOMATED VARIABLES */ 
$upload_dir = "images/external/doodles/"; 
$url   = md5(uniqid(rand(), true)); 
$unique_post_id = md5(uniqid(rand(), true)); 
$timestamp  = time(); 
$nature   = "doodle"; 
$imageUrl  = $upload_dir.$url.'.png'; 

$img = $_POST['data']; 
$img = substr($img,strpos($img,",")+1); 
$data = base64_decode($img); 
$file = $upload_dir . $url . ".png"; 
$success = file_put_contents($file, $data); 


if(!$success) { 

    exit('unable to upload'); // Prints success and exit the script 

} else { 

    exit($file); // Prints success and exit the script 

} 

?> 

UPDATE:

ich Erfolg und Fehler zu Ajax und es kommt als Fehler .:

function doodleSave() { 
    var canvas = document.getElementById("doodle-canvas"); 
    var canvasData = canvas.toDataURL("image/png"); 
    $.ajax({ 
    url:'test.php', 
    type:'POST', 
    data:{ data:canvasData }, 
     success: function(){ 
     alert('success'); 
     }, 
     error: function(){ 
     alert('failure'); 
     } 
    }); 
} 
+0

Wenn Sie die Anfrage in Ihrer Netzwerkkonsole betrachten, welcher HTTP-Antwortcode wird die XHR-Anfrage erhalten? Mir ist nicht bewusst, wie sich exit() auf den Antwortcode der Anfrage auswirkt, daher ist es möglich, dass ein Fehlerantwortcode zurückgegeben wird. In diesem Fall sollten Sie 'header()' verwenden, um den Anforderungsantwortcode anzugeben, anstatt exit() zu verwenden. wie 'header (" HTTP/1.0 200 Ok ")' oder 'header (" HTTP/1.0 400 Bad Request ")' – Taplar

+0

Ich schaute auf die XHR-Antworten und sie sind alle für meine Benachrichtigungsprüfung, die alle 10 Sekunden ausgeführt wird. Nichts auf dieser Upload-Skript – Sam

+0

Aber die Dinge sparen noch in das Verzeichnis, wenn ich die doodleSave Taste – Sam

Antwort

1

Verwenden json_encode Daten mit AJAX zurück zu senden.

<?php 

/* AUTOMATED VARIABLES */ 
$upload_dir = "images/external/doodles/"; 
$url   = md5(uniqid(rand(), true)); 
$unique_post_id = md5(uniqid(rand(), true)); 
$timestamp  = time(); 
$nature   = "doodle"; 
$imageUrl  = $upload_dir.$url.'.png'; 

$img = $_POST['data']; 
$img = substr($img,strpos($img,",")+1); 
$data = base64_decode($img); 
$file = $upload_dir . $url . ".png"; 
$success = file_put_contents($file, $data); 
if(!$success) { 

    echo json_encode(['filename' => false]); 
    exit(); // Prints success and exit the script 

} else { 

    echo json_encode(['filename' => $file]); 
    exit(); 
} 

?> 

In Ihrer AJAX tun dies

function doodleSave() { 
    var canvas = document.getElementById("doodle-canvas"); 
    var canvasData = canvas.toDataURL("image/png"); 
    $.ajax({ 
     url:'test.php', 
     type:'POST', 
     data:{ data:canvasData }, 
     success: function(response){ 
      var data = JSON.parse(response); 
      if (data.filename != false) { 
       alert(data.filename); 
      }else { 
       alert('unable to upload'); 
      } 
     }, 
     error: function(){ 
      alert('failure'); 
     } 
    }); 
} 

Sie auch $.post(), die eine Abkürzung für $.ajax() für POST Anfrage nutzen können.

function doodleSave() { 
    var canvas = document.getElementById("doodle-canvas"); 
    var canvasData = canvas.toDataURL("image/png"); 

    $.post('test.php', {data: canvasData}, function (response) { 
     var data = JSON.parse(response); 
     if (data.filename != false) { 
      alert(data.filename); 
     } else { 
      alert('unable to upload'); 
     } 
    }) 
} 
+0

Ich habe dieses Wort für Wort und ich nicht getan bekomme eine Warnung. Ich habe die Konsole überprüft und es antwortet mit 200 – Sam

+0

ist Ihre PHP-Datei 'test.php'? – julekgwa

+0

Ich habe die gesamte http: //www.website..itc verwendet. Habe es nur auf den Namen und die Erweiterung geändert und es funktioniert gut! Danke – Sam

0

Sie müssen alle ersetzen Leerzeichen mit Pluszeichen (+).

Ihr Code mit einem fix:

<?php 

/* AUTOMATED VARIABLES */ 
$upload_dir = "images/external/doodles/"; 
$url   = md5(uniqid(rand(), true)); 
$unique_post_id = md5(uniqid(rand(), true)); 
$timestamp  = time(); 
$nature   = "doodle"; 
$imageUrl  = $upload_dir.$url.'.png'; 

$img = $_POST['data']; 
$img = substr($img,strpos($img,",")+1); 
$img = str_replace(' ', '+', $img); // Here is a fix 
$data = base64_decode($img); 
$file = $upload_dir . $url . ".png"; 
$success = file_put_contents($file, $data); 


if(!$success) { 

    exit('unable to upload'); // Prints success and exit the script 

} else { 

    exit($file); // Prints success and exit the script 

} 

?> 
+0

Erhalten Sie immer noch keine Antwort für die Variable $ file – Sam

+0

@Sam das Bild wird hochgeladen und erfolgreich gespeichert? –

+0

Es hochgeladen erfolgreich, aber ich muss den Standort zurück zu AJAX – Sam