在ajax中没有得到警报格式的响应

时间:2014-07-25 06:35:00

标签: javascript php ajax json jquery-plugins

我正在将plugin用于我的某个网站。我希望alert()响应是成功还是错误。我试图将代码放在script.js上,但是没有成功我这样累的代码:

done: function (e, data) {
           alert(data.status); // also like data['status');
            data.context.text(data.Status);
        },

我的PHP代码以这种json格式返回响应。这段代码在firebug上工作正常但不在屏幕上。

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;
  }

1 个答案:

答案 0 :(得分:1)

sample link

的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;