看下面的代码,插入和更新所有工作正常但我无法获得成功消息,我得到这样的错误
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
如何删除错误以及如何获取成功消息
<script type="text/javascript">
$(document).ready(function(){
$("#user-submit").click(function(event){
event.preventDefault();
if($("form#newUserForm").valid()){
var formData = new FormData();
var formData = new FormData($('#newUserForm')[0]);
formData.append('file', $('input[type=file]')[0].files[0]);
$.ajax({
url: 'horoscope-check.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
var res=jQuery.parseJSON(data);// convert the json
console.log(res);
},
});
return false;
}else{
console.log("false");
}
});
});
</script>
<?php
include("dbconfig.php");
$ssmid = "SSM84175";
$filename = basename($_FILES['file']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new_name= md5($filename.time()).'.'.$extension;
$approved = 0;
$approved_on = date('Y-m-d H:i:s');
$updated_on = date('Y-m-d H:i:s');
$status = 0;
if (move_uploaded_file($_FILES['file']['tmp_name'], "horoscope/".$new_name)) {
// FILE TYPE CHECKING
$allowed = array('gif','png' ,'jpg');
if(!in_array($extension,$allowed) ) {
$horoscope = array("message" => "filetype error");
echo json_encode($horoscope);
}
else{
if($_FILES['file']['size']>2459681){
$horoscope = array("message" => "filesize error");
echo json_encode($horoscope);
}
else{
//echo "filesize(success)";
$sql=mysql_query("SELECT * FROM user_horoscope WHERE ssm_id='$ssmid'");
$count = mysql_num_rows($sql);
if($count != 0){
$horoscope = array("Count" => $count);
$sql=mysql_query("UPDATE user_horoscope SET horoscope='$new_name',updated_on='$updated_on' WHERE ssm_id='$ssmid'");
if($sql){
$horoscope = array("message" => "update success");
}else{
$horoscope = array("message" => "update error");
}
echo json_encode($horoscope);
}else{
//$horoscope = array("message" => 'error');
$sql = mysql_query("INSERT INTO user_horoscope (ssm_id,horoscope,approved,approved_on,status) VALUES ('$ssmid','$new_name','$approved','$approved_on','$status')");
if($sql){
$horoscope = array("message" => "successully insert");
}else{
$horoscope = array("message" => "insert error");
}
echo json_encode($horoscope);
}
echo json_encode($horoscope);
}
}
}else{
//echo "Error";
$horoscope = array("message" => "file is not moving");
echo json_encode($horoscope);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Horoscope</title>
<!-- Bootstrap 3.3.5 -->
<link rel="stylesheet" href="../horoscope/bootstrap.min.css">
<link rel="stylesheet" href="../horoscope/_all-skins.min.css">
<!-- Admin Extension CSS -->
<link href="http://preview.oklerthemes.com/porto-admin/edge/assets/stylesheets/theme-admin-extension.css" rel="stylesheet">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- Admin Extension Skin CSS -->
<link rel="stylesheet" href="http://preview.oklerthemes.com/porto-admin/edge/assets/vendor/bootstrap-datepicker/css/bootstrap-datepicker3.css" />
<link href="http://preview.oklerthemes.com/porto-admin/edge/assets/stylesheets/skins/extension.css" rel="stylesheet">
<!-- Admin Extension Specific Page Vendor CSS -->
<link rel="stylesheet" href="http://preview.oklerthemes.com/porto-admin/edge/assets/vendor/bootstrap-fileupload/bootstrap-fileupload.min.css" />
</head>
<body>
<form class="form-horizontal form-bordered" method="POST" id="newUserForm" enctype="multipart/form-data">
<div class="form-group">
<label class="col-md-3 control-label">Photo Upload</label>
<div class="col-md-6">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input">
<i class="fa fa-file fileupload-exists"></i>
<span class="fileupload-preview"></span>
</div>
<span class="btn btn-default btn-file">
<span class="fileupload-exists">Change</span>
<span class="fileupload-new">Select file</span>
<input type="file" id="file" name="file" value="" aria-required="true" required="" data-msg-required="Please select your file">
</span>
<a href="#" class="btn btn-default fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-info" type="submit" id="user-submit">Submit</button>
</div>
</div>
</form>
<!-- jQuery 2.1.4 -->
<script src="../horoscope/jQuery-2.1.4.min.js"></script>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script><script>-->
<script src="http://preview.oklerthemes.com/porto-admin/edge/assets/vendor/bootstrap-fileupload/bootstrap-fileupload.min.js"></script>
<!-- Admin Extension Specific Page Vendor -->
<script src="http://preview.oklerthemes.com/porto-admin/edge/assets/vendor/jquery-validation/jquery.validate.js"></script>
<script src="../horoscope/jquery.validation.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
</body>
</html>
答案 0 :(得分:0)
您的代码调用echo json_encode($horoscope);
两次。
...
else{
//echo "filesize(success)";
$sql=mysql_query("SELECT * FROM user_horoscope WHERE ssm_id='$ssmid'");
$count = mysql_num_rows($sql);
if($count != 0){
$horoscope = array("Count" => $count);
$sql=mysql_query("UPDATE user_horoscope SET horoscope='$new_name',updated_on='$updated_on' WHERE ssm_id='$ssmid'");
if($sql){
$horoscope = array("message" => "update success");
}else{
$horoscope = array("message" => "update error");
}
echo json_encode($horoscope);
}else{
//$horoscope = array("message" => 'error');
$sql = mysql_query("INSERT INTO user_horoscope (ssm_id,horoscope,approved,approved_on,status) VALUES ('$ssmid','$new_name','$approved','$approved_on','$status')");
if($sql){
$horoscope = array("message" => "successully insert");
}else{
$horoscope = array("message" => "insert error");
}
echo json_encode($horoscope);
}
// the next line should be commented out.
// echo json_encode($horoscope);
}
...
看看这是否解决了这个问题。