简体   繁体   English

Uncaught SyntaxError:意外的令牌W-parseJSON错误

[英]Uncaught SyntaxError: Unexpected token W - parseJSON error

So my problem is that I am trying to have this IF statement run once the file is confirmed transfered and uploaded. 所以我的问题是,一旦确认文件已传输并上传,我将尝试运行此IF语句。 The only problem is that only 1 of these actions run. 唯一的问题是这些操作中只有1个运行。 I have a loading bar that is supposed to reach 100% when the upload is finished. 我有一个加载栏,上传完成后应该达到100%。 I get this in my browser: Uncaught SyntaxError: Unexpected token W - jquery-1.6.3.min.js:2 e.extend.parseJSON jquery-1.6.3.min.js:2 xhr.onload .. however, if I use 我在浏览器中得到了: 未捕获的SyntaxError:意外的令牌W-jquery-1.6.3.min.js:2 e.extend.parseJSON jquery-1.6.3.min.js:2 xhr.onload ..但是,如果我采用

INSERT INTO 

before 之前

exit_status('File was uploaded successfuly!');

The progressbar simply does not appear to run to 100%. 进度栏似乎根本无法运行到100%。 However, if I put it first, it does reach 100%, but I do not insert anything into my SQL table. 但是,如果我把它放在第一位,它的确达到了100%,但是我没有在SQL表中插入任何内容。 How can I fix this? 我怎样才能解决这个问题? and what is my mistake here? 那我的错是什么?

 <?php
    // If you want to ignore the uploaded files,
    // set $demo_mode to true;

    $demo_mode = false;
    $upload_dir = '../img/gallery/';
    $allowed_ext = array('jpg','jpeg','png','gif');


    if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
        exit_status('Error! Wrong HTTP method!');
    }


    if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){

        $pic = $_FILES['pic'];

        if(!in_array(get_extension($pic['name']),$allowed_ext)){
            exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
        }

        if($demo_mode){

            // File uploads are ignored. We only log them.

            $line = implode('       ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
            file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);

            exit_status('Uploads are ignored in demo mode.');
        }


        // Move the uploaded file from the temporary
        // directory to the uploads folder:
        $db = new PDO('mysql:DB INFO);
        $menuitemcount = $db->prepare("SELECT id FROM bilder");
        $menuitemcount->execute();
        $menucount = $menuitemcount->rowCount();

        $newpicturename = $menucount.$pic['name'];

        if(move_uploaded_file($pic['tmp_name'], $upload_dir.$newpicturename)){
             date_default_timezone_set('Europe/Oslo');
             $namepic = $pic['name'];
             $urlpic = "../img/gallery/".strval($menucount).$pic['name'];
             $altpic = $pic['name'];
             $datepic = date(Y,m,d);

             $query = $db->prepare("INSERT INTO bilder (name,url,alt,date) VALUES                (:name,:url,:alt,:date)");
             $query->execute(array(':name' => $namepic, ':url' => $urlpic, ':alt' => $altpic, ':date' => $datepic));
             exit_status('File was uploaded successfuly!');

        }
    }

    exit_status('Something went wrong with your upload!');



    // Helper functions

    function exit_status($str){
        echo json_encode(array('status'=>$str));
        exit;
    }

    function get_extension($file_name){
        $ext = explode('.', $file_name);
        $ext = array_pop($ext);
        return strtolower($ext);
    }
?>

This is what I see when the INSERT operation on top: 这是当INSERT操作出现在顶部时的结果: SQL工作,文件上传工作,但负载栏不是100%

This is when I change the spot for the insert operation and the exit_status: 这是当我更改插入操作和exit_status的位置时: 100%加载,但SQL和UPLOAD不起作用

FULL SCRIPTS: 完整脚本:

http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/assets/js/script.js http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/assets/js/jquery.filedrop.js http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/assets/js/script.js http://demo.tutorialzine.com/2011/09/html5-file-upload -jquery-php / assets / js / jquery.filedrop.js

POSSIBLY CAUSING THE ERROR: 可能导致错误:

xhr.onload = function() { 
                if (xhr.responseText) {
                var now = new Date().getTime(),
                    timeDiff = now - start_time,
                    result = opts.uploadFinished(index, file, jQuery.parseJSON(xhr.responseText), timeDiff);
                    filesDone++;
                    if (filesDone == files_count - filesRejected) {
                        afterAll();
                    }
                if (result === false) stop_loop = true;

You might have a syntax error in your SQL query because date is a reserved keyword. 您的SQL查询中可能存在语法错误,因为date是保留关键字。 You should always enclose your table and column names with backticks. 您应该始终在表名和列名两侧加上反引号。

INSERT INTO bilder (`name`,`url`,`alt`,`date`) VALUES (:name,:url,:alt,:date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM