Home  >  Q&A  >  body text

javascript - 为什么 ajaxFileUpload 上传成功后却执行 error

使用 ajaxFileUpload 插件上传图片的时候,在 UC 浏览器下会出现问题:

图片上传成功了,能返回生产的图片 URL,但是却执行 error 报错,为什么呢?

$.ajaxFileUpload({
    url:'/group_action/upload_image',
    secureuri:false,
    fileElementId:'pub_upload_img',
    dataType: 'json',
    success: function (d){
        if(d.result == 1){
            alert(d.url);
        }else{
            alert(d.msg);
        }
    },
    error: function(data, status, e){
        alert(data.responseText);
    }
});
迷茫迷茫2719 days ago960

reply all(6)I'll reply

  • 巴扎黑

    巴扎黑2017-04-10 15:52:57

    alert(d)看看,你确定是json返回结果吗?

    参考我的:

    <script>
    $(document).ready(function(){
        $('#test').click(function(){
            $.ajaxFileUpload({
                  url:'{:U("Index/upload")}',
                  secureuri: false,
                  fileElementId:'file',
                  dataType: 'text',//返回数据类型
                  success: function (data, status){
                      //alert(data);
                      $("#img").attr("src","http://127.0.0.1/6/0604/Uploads/2015-06-06/"+data);
                  },
                  error: function (data, status, e)//服务器响应失败处理函数
                  {
                      alert(e);
                  }
                });
            //$('#upload').submit();
        });
    });
    </script>
    

    http://my.oschina.net/rain21/blog/425565

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 15:52:57

    检查后端返回的数据是什么。

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 15:52:57

    我也遇到这个问题了,但是断点总是先跑到error中,然后进success,在success里返回的data为空,但是网络中返回的有值,求大神助攻

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-10 15:52:57

    在你的ajaxfileupload.js找到这个函数,直接替换就可以了,前提保证你服务器反馈过来的是json格式。亲测,没有问题。

        uploadHttpData: function( r, type ) {
                var data = !type;
                data = type == "xml" || data ? r.responseXML : r.responseText;
                // If the type is "script", eval it in global context
                if ( type == "script" )
                    jQuery.globalEval( data );
                // Get the JavaScript object, if JSON is used.
                if ( type == "json" )
                {
                    // If you add mimetype in your response,
                    // you have to delete the '<pre></pre>' tag.
                    // The pre tag in Chrome has attribute, so have to use regex to remove
                    var data = r.responseText;
                    var rx = new RegExp("<pre.*?>(.*?)</pre>","i");
                    var am = rx.exec(data);
                    //this is the desired data extracted
                    var data = (am) ? am[1] : "";    //the only submatch or empty
                    
                    var position = data.indexOf('}')+1;
                    data = data.substring(0,position);
                    
                    eval( "data = " + data );
                }
                // evaluate scripts within html
                if ( type == "html" )
                    jQuery("<p>").html(data).evalScripts();
                //alert($('param', data).each(function(){alert($(this).attr('value'));}));
                return data;
            }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 15:52:57

    朋友,我也遇到了这个问题,用了一天时间最终解决了,都是uc浏览器坑的,返回的json里面多了一段uc的js,现付解决方法,希望能帮到你:
    if (type == "json"){

            ***var position = data.indexOf('}')+1;
            data = data.substring(0,position);***
            eval("data = " + data);
        }
        加粗的那两句放进相应的ajaxfileupload.js文件中就行了,就是截取了有效的json,保证是合法的json字符串

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 15:52:57

    可能是你上传的图片太大了,超时了吧,你把超时时间设置长点试试~

    reply
    0
  • Cancelreply