[英]Didn't get response on alert formate in ajax
我正在我的網站之一上使用此插件 。我想alert()
是成功還是錯誤的響應。我試圖將代碼放在script.js上,但沒有成功。
done: function (e, data) {
alert(data.status); // also like data['status');
data.context.text(data.Status);
},
我的php代碼以此json格式返回響應。此代碼在螢火蟲上正常工作,但在屏幕上無法正常工作。
if (file_exists($targetFolder. '/' .$_FILES['upl']['name'])) {
echo '{"status":"Image already exist "}';
exit;
}
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"Extension not allowed"}';
exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'],
$targetFolder. '/' .$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
的script.js
done: function (e, data) {
//console.log(data.status); // also like data['status');
console.log(data.result); // "{"status":"Extension not allowed"}"
var result = jQuery.parseJSON(data.result );
console.log(result.status); // "Extension not allowed"
if (result.status != "success")
{
//data.context.addClass('error'); //file Name color is "RED"
data.context.text(result.status);
}
},
upload.php的
<?php
// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0) {
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
if (file_exists('./uploads/' .$_FILES['upl']['name'])) {
echo '{"status":"Image already exist "}';
exit;
}
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"Extension not allowed"}';
exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], './uploads/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.