我想动态添加照片,但我遇到了问题,我很困惑在哪里修复此错误。
此错误是:
SyntaxError:JSON.parse:JSON数据第1行第1列意外的数据结尾
这是我的网页 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>
这是我的PHP crud.php
<?php
include "../element/connection.php";
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 :(得分:1)
这是错误的,
dataSlide = JSON.parse(response);
在php中,你回复OK
或NOK
,你无法使用解析。
也在php
这不起作用
return $destination_url;
相反,
echo json_encode($destination_url);
答案 1 :(得分:1)
您需要创建正确的JSON响应&#34; echo&#39; ok&#39;;&#34;或者&#34;回声&#39; nook&#39;;&#34;
OPEN_BRACKET, INTEGER, CLOSE_BRACKET
此外,您还需要将标题设置为&#39; application / json&#39;:
echo json_encode('ok/nook');