我正在处理用户同时发送图像和评论的表单。无论发生什么,ajax调用直接导致错误(即使文件和注释成功上传并写入DB)。
我尝试记录错误的反馈,这就是我收到的:
Object {readyState: 4, responseText: "<pre class='xdebug-var-dump' dir='ltr'>↵<b>array</…DstagramPost succesfully made.{"check":"success"}", status: 200, statusText: "OK"}
这是我的ajax电话:
<script type="text/javascript">
$(document).ready(function(){
$("#postSubmit").on("click", function(e){
$.ajax({
url: "ajax/postUpload.php",
type: "POST",
data: new FormData($("#postForm")[0]),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function(data)
{
console.log(data);
},
error: function (request, status, error) {
console.log(request);
alert('Something is wrong');
}
});
e.preventDefault();
});
});
</script>
我的PHP代码:
<?php
include_once("../classes/Post.class.php");
session_start();
$post = new Post();
var_dump($_FILES);
if(!empty($_POST)){
$file_tmp_name = $_FILES['postImage']['tmp_name'];
$post->checkIfImage();
$post->checkFileSize();
$post->checkFileFormat();
$post->checkAspectRatio();
if(!$post->checkIfImage()){
$uploadStatus['image'] = "false";
}
if(!$post->checkFileSize()){
$uploadStatus['size'] = "false";
}
if(!$post->checkFileFormat()){
$uploadStatus['format'] = "false";
}
if(!$post->checkAspectRatio()){
$uploadStatus['ratio'] = "false";
}
if($post->checkIfImage() && $post->checkFileSize() && $post- >checkFileFormat() && $post->checkAspectRatio()){
$result = $post->createPost($file_tmp_name);
$uploadStatus['check'] = "success";
}else{
$uploadStatus['check'] = "error";
}
header('Content-Type: application/json; charset=utf-8', true);
echo json_encode($uploadStatus);
}
?>
总而言之,如果图像通过了checkIfImage()等所有要求,它就会上传到文件夹,图像路径和描述会在数据库中发布。如果它没有通过所有要求,它就不会在那里发布。但是我每次都会收到错误。
如果我删除了dataType:&#39; json&#39;部分它会出错,如果我将其更改为其他任何内容,我会得到以下输出:
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=1)</i>
'postImage' <font color='#888a85'>=></font>
<b>array</b> <i>(size=5)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'passfoto.png'</font> <i>(length=12)</i>
'type' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'image/png'</font> <i>(length=9)</i>
'tmp_name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'D:\xampp\tmp\phpDFE9.tmp'</font> <i>(length=24)</i>
'error' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
'size' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>112825</font>
</pre>D:\xampp\htdocs\IMDstagramPost succesfully made.{"check":"success"}
我需要的部分当然是检查,所以我可以例如清除表格如果检查=成功或如果不是则给出错误..
答案 0 :(得分:3)
问题是因为您在private void CreateZipContentFolder(string zipsPath, string destinationPath) {
Zips = Directory.GetFiles(zipsPath, "*.zip", SearchOption.TopDirectoryOnly).ToList();
if (Zips.Count != 0) {
MyLog.WriteToLog("Creating Folder of ZipFiles... From: " + zipsPath + " To: " + destinationPath, MyLog.Messages.Info);
foreach (string zip in Zips) {
FileInfo fileInfo = new FileInfo(zip);
string dirName = destinationPath + "\\" + fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
using (ZipArchive archive = ZipFile.OpenRead(zip)) {
foreach (ZipArchiveEntry entry in archive.Entries) {
if (entry.FullName.EndsWith("/")) {
try {
ZipFile.ExtractToDirectory(zip, destinationPath);
} catch (IOException e) {
MyLog.WriteToLog(e.Message, MyLog.Messages.Error);
}
break;
} else if (new FileInfo(dirName).Exists == false) {
try {
Directory.CreateDirectory(dirName);
ZipFile.ExtractToDirectory(zip, dirName);
} catch (IOException e) {
MyLog.WriteToLog(e.Message, MyLog.Messages.Error);
}
break;
}
}
}
}
MyLog.WriteToLog("Created Temporary Folders", MyLog.Messages.Info);
} else { MyLog.WriteToLog("No Zips Found in: " + zipsPath, MyLog.Messages.Warning); }
}
JSON之前在PHP文件中输出了一些HTML。您可以在JSON echo
参数中看到它:
response
删除该HTML。响应文本应仅 此JSON格式的字符串:
<pre class='xdebug-var-dump' dir='ltr'>↵<b>array</…DstagramPost succesfully made.