Ajax图像上载导致Internet Explorer中出现意外输出


Ajax image upload causes unexpected output in Internet Explorer

我已经处理了一个上传图片的ajax请求,并使用了以下代码。

<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="upl"/>
</form>
<script> // all other jquery dependencies are added for ajax file upload 
$(function(){
      $('#upload').fileupload({
      add: function (e, data) {
           var jqXHR = data.submit();    
        },
      success:function(result){
                alert(result);
     }
  });
});
</script>

我的upload.php

<?php if(isset($_FILES['upl'])){
      $file = 'some random name'; // generated using rand functions in php
     if(move_uploaded_file($_FILES['upl']['tmp_name'],$file ))
        echo 'success';
      }
?>

成功后,我希望jQuery能提醒success,它在chrome和其他浏览器中运行得很好,但当涉及到IE 7时,它会提醒[object Object]作为输出,但我希望它是success

我在哪里犯了错误?

在upload.php中,而不是使用回显输出

echo 'success';

您可以使用函数JSON_encode返回对象的JSON表示。

通过这种方式,您可以使用javascript解析json,并在警报中使用解析后的字符串。此方法比您正在使用的方法更干净,并且可能与旧版本的IE兼容(您可能还需要使用JSON.stringify函数将解析的JSON转换为字符串)。

可能是JSON对象未在IE8和IE7 中创建

在页面中添加JSON.js文件