无法获得JSON响应

时间:2015-12-08 18:31:02

标签: javascript php jquery json ajax

我想动态添加照片,但我遇到问题,我没有得到JSON响应(响应标签中没有任何内容):

enter image description here

这些数据和图像未插入数据库中,我很困惑。

这是我的 index.html

<?php  
    //Foto_ID
    $dr      = (rand(100,10000)); 
    $ymdhis  = date("ymdhis");
    $rd      = $dr.$ymdhis;
?>
<div id="main-content2">
        <script>
        var maxSlide = 5;
        var curSlide = 1;
        var Ids = 1;
        var ajaxCheckInterval = "";
        function readImage(input,ids) 
        {
            if (input.files && input.files[0]) 
            {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#img_'+ids).attr('src', e.target.result);
                }
                reader.readAsDataURL(input.files[0]);
            }
        }
        function generateSlide()
        {
            if( curSlide <= maxSlide )
            {

                var  html ='<br/><div id="slideAdd_'+Ids+'" >';
                     html+=' <form action="#" id="slide_'+Ids+'" class="form-horizontal" enctype="multipart/form-data">';
                     html+='    <div class="form-group">';
                     html+='            <label class="col-sm-3 control-label">Foto Barang</label>';
                     html+='            <div class="col-sm-9">';
                     html+='                <input type="hidden" name="ft_'+Ids+'"   id="ft_'+Ids+'" value="<?php echo $rd ?>">';
                     html+='                <input type="hidden" name="ur_'+Ids+'"   id="ur_'+Ids+'" value="'+Ids+'" >';
                     html+='                <input  onChange="readImage(this,'+Ids+')" type="file" id="foto_'+Ids+'" name="foto_'+Ids+'" />';
                     html+='                    <img src="images/noimage.jpg" id="img_'+Ids+'" style="width: 300px; height: 250px;"  />';
                     html+='            </div>';
                     html+='    </div>';
                     html+='    <div class="form-group">';
                     html+='            <div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2">';
                     html+='                <button onClick="removeSlide(\''+Ids+'\'); return false;" class="btn btn-danger"><i class="fa fa-times"></i> Remove</button>';
                     html+='            </div>';
                     html+='    </div>';
                     html+=' </form>';
                     html+='</div><br/>';
                $("#main-content2").append(html);
                curSlide++;
                Ids++;
            }
            else
            {

            }
        }
        function removeSlide(Ids)
        {
            $('#slideAdd_'+Ids).slideUp('slow');
            setTimeout(function(){ $('#slideAdd_'+Ids).remove(); }, 2000);
            curSlide--;
        }
        function getSlide()
        {
            showLoading("show");
            ajaxCheckInterval = setInterval(function(){ redirectMe() }, 1000);
            var a = 1;
            for( var i = 0; i <= Ids; i++ )
            {
                try{

                    var formData = new FormData();
                        formData.append("file", $( '#foto_'+i )[0].files[0]);
                        formData.append("ft", $( '#ft_'+i ).val()) ;
                        formData.append("ur", $( '#ur_'+i ).val()) ;
                    uploadSlide(formData,i,Ids);
                    a++;
                }catch(e){

                }

            }       
        }
        function redirectMe()
        {
            if($.active == 0){
                setTimeout(function(){ 
                    showLoading("show"); 
                    myStopFunction();
                    window.location.href = "barang";
                }, 1000);
            }
        }
        function myStopFunction() {
            clearInterval(ajaxCheckInterval);
        }
        function uploadSlide(formData,x,Ids2)
        {
            $.ajax({
                url: 'crud.php?type=Foto_Barang',  
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function(response) {
                    if( response != "OK" )
                    {
                        // Error In Here
                        dataSlide = JSON.parse(response);
                    }   
                } 
            });
        }
        function showLoading( type )
        {
            if( type == "show" )
            {
                var html = '';
                    html += '<div id="loader">';
                    html += '<div id="loadOver" class="loadOver"></div>';
                    html += '<div class="loading">';
                    html += '<img src="images/animatedCircle.gif" />';
                    html += '</div></div>';  
                $('body').append(html);
            }
            else
            {
                $('#loader').remove();
            }
        }
            </script>   
                    </div>
                <center>
                <button type="button" class="btn btn-success" onClick="generateSlide()" ><i class="fa fa-plus"></i> Add Photos (Maks : 5)</button>
                <button onClick="getSlide()"   class="btn btn-primary"><i class="fa fa-check"></i> Save</button>
                </center>

这是我的 crud.php

<?php
include "../element/connection.php";
header('Content-Type: application/json');
switch ($_REQUEST['type'])
{
    case "Foto_Barang":
    {
        $foto_id     = $_REQUEST['ft'];
        $urut        = $_REQUEST['ur'];
        $path        = '../images/barang/'; 
        $url = $path.$foto_id."_".$_FILES["file"]["name"];  
                if($_FILES['file']['size'] < 500000) // 500 kb
                     {
                        move_uploaded_file($_FILES["file"]["tmp_name"],$url);
                     }
                else {
                        function compress_image($source_url, $destination_url, $quality)    
                        {       
                                $info = getimagesize($source_url);          
                                if ($info['mime'] == 'image/jpeg')                  
                                    $image = imagecreatefromjpeg($source_url);          
                                elseif ($info['mime'] == 'image/gif')                  
                                    $image = imagecreatefromgif($source_url);           
                                elseif ($info['mime'] == 'image/png')                   
                                    $image = imagecreatefrompng($source_url);                           

                                    imagejpeg($image, $destination_url, $quality);      
                                    return $destination_url;    
                        }   
                            compress_image($_FILES["file"]["tmp_name"], $url, 20);  
                    }
                $sql ="INSERT INTO foto_barang
                (foto_id,foto,urut) VALUES ('".$foto_id."','".$foto_id."_".$_FILES["file"]["name"]."','".$urut."')";
                if( mysqli_query($con,$sql) ){
                    echo "OK";
                }else{
                    echo "NOK";
                }

        break;
    }
}
    ?>

0 个答案:

没有答案