lundi 29 juin 2015

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data on my Image Uploader


Im new here and did not find out my Solution by using Google or StackOverflow.
My Problem is that i have a Multiple Image Uploader that works with JSON, AJAX and PHP... But while I am programming, i get this Message on the Firefox Console:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Witch is related to this line:

ajax = function (data) {
    var xml = new XMLHttpRequest(), uploaded;
    xml.addEventListener("readystatechange", function() {
        if(this.readyState === 4) {
            if(this.status === 200) {
                uploaded = JSON.parse(this.response); //THIS IS THE ERROR LINE
                if(typeof o.options.finished === "function") {
                    o.options.finished(uploaded);
                }
            }else {
                if(typeof o.options.error === "function") {
                    o.options.error();
                }
            }
        }
    });

I think its because the Array in the Variable "Uploaded" is empty... But it shouldve be filled so far.. Well, here is the PHP Code:

header("Content-Type: application/json");

$uploaded = [];
$allowed = ["mp4", "jpg", "png", "jpeg", "doc", "avi"];

$files = $_FILES["file"];

$succedeed = [];
$failed = [];

if (!empty($files)) {
    foreach($files["name"] as $key => $name) {
        if($files["error"][$key] === 0) {

            $temp = $files["tmp_name"][$key];

            $ext = explode(".", $name);
            $ext = strtolower(end($ext));

            $file = $files["tmp_name"][$key] . $ext;


            if(in_array($ext, $allowed) === true && move_uploaded_file($temp, "uploads/{$file}") === true) {
                $succedeed[] = array(
                    "Original Name" => $name,
                    "MD5 Name" => $file
                );
            }else {
                $failed[] = array(
                    "Original Name" => $name
                );

            }   
        }
    }

    if(!empty($_POST["ajax"])) {
        echo json_encode(array(
            "Erfolgreich hochgeladen" => $succedeed,
            "Nicht erfolgreich hochgeladen" => $failed
        ));
    }


}

Im very Desperated because of this... :D Hope someone can Help me... ^^

Niggo


Aucun commentaire:

Enregistrer un commentaire