dougao7801 2016-11-20 03:47
浏览 153

parsererror SyntaxError:JSON输入的意外结束

When I add if(isset($_POST['uplprofimg'])), I get the error:

parsererror SyntaxError: Unexpected end of JSON input

If I exclude the php if isset post function, the code works perfectly.

So, PHP:

if(isset($_POST['uplprofimg'])){ //← This  if broke my code >:(
        //Works if I remove the ↑
        if($_FILES['imagefile']['size'] > 5242880){
            $ress = "<div class='error'>Max file size is 5MB</div>";
            echo json_encode(array('response' => false,'ress' => $ress));
        }else{
            $imgtype = pathinfo($_FILES['imagefile']['name'],PATHINFO_EXTENSION);
            if(!in_array($imgtype,array('jpg','jpeg','png','gif'))){
                $ress = "<div class='error'>Only <b>jpg</b>, <b>jpeg</b>, <b>png</b> and <b>gif</b> files are allowed (".$imgtype.")</div>";
                echo json_encode(array('response' => false,'ress' => $ress));
            }else{
                $newimgname = "/profile_picture/".random_num($length = 8).time().random_num($length = 8).".".$imgtype;
                $newimgnameserv = $_SERVER['DOCUMENT_ROOT'].$newimgname;
                if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$newimgnameserv)){
                    $upuser = $mysqli->prepare('UPDATE accinfo SET profilepic = ? WHERE username = ?');
                    $upuser->bind_param('ss',$newimgname,$username);
                    $check = $upuser->execute();
                    if($check == true){
                        echo json_encode(array('response' => true));
                    }else{
                        $ress = "<div class='error'>SQL Error</div>";
                        echo json_encode(array('response' => false,'ress' => $ress));   
                    }
                }else{
                    $ress = "<div class='error'>Couldn't move the file. Please try again later.(".$newimgname.")</div>";
                    echo json_encode(array('response' => false,'ress' => $ress));
                }
            }
        }
    }

and JS:

$("#uploadimgform").on("submit",function(event){
    event.preventDefault();

    var formData = new FormData(this);

    $("#uploadlistener").html("<img src='/images/load.gif' width='50px' />");

    $.ajax({
        type: 'POST',
        url: "/system/requests.php",
        data: formData,
        dataType: "json",
        cache: false,
        contentType: false,
        processData: false,
        success: function(data){
            if(data.response === true){
                $("#bodyfader").fadeOut("slow");
                $("#expandedupl").fadeOut("slow");
                $("#uploadlistener").html("");
            }else{
                $("#uploadlistener").html(data.ress);
                $('#profilepic').css('background', 'url(/images/defaultprof.png)');
                $("#profilepic").css("background-size","cover");
                $("#profilepic").css("background-position","center");
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    });
});

If I remove the if(isset($_POST['uplprofimg'])){...}, the code works. But if I add it there, I get the json error. What is going on?

HTML Form:

<form method='POST' enctype="multipart/form-data" id='uploadimgform' style='display: none;'>
     <input type='file' id='upload' name='imagefile' accept="image/x-png,image/gif,image/jpeg" />
     <input type='submit' id='uploadconf' name='uplprofimg' />
</form>

This is how the form is submitted:

$("#upload").change(function(){
    if(this.files[0].size > 5242880){
        $("#uploadlistener").html("<div class='error'>Maximum file size is 5MB.</div>");
        $("#upload").val("");   
    }else{
        $("#uploadimgform").submit();
        readURL(this);
    }
});

I have no idea why, but I did this:

formData.append('uplprofimg',1);

and the code works. Someone can explain this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?