I believe that everyone often uses file upload operations in their work. Because I am working on the front-end, I mainly introduce the operations of ajax in the front-end. I omitted a lot of code, just take js
$.ajaxFileUpload({ url:'www.coding/mobi/file/uploadSingleFile.html',//process image script secureuri :false, fileElementId :'image2',//file control id. It is input type="file" id="image2" dataType : 'json', success : function (data, status){ console.log(data); }, error: function(data, status, e){ alert(e); } })According to the tutorial, there is no problem if you upload this way, but it has always reported an error. I forgot what was reported. I'm sorry, because I remember to make up for this article after using it for a long time, but if I need to modify its source code, the error can be solved.
The last paragraph of its source code looks like this
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" ) eval( "data = " + data ); // evaluate scripts within html if ( type == "html" ) jQuery("<div>").html(data).evalScripts(); //alert($('param', data).each(function(){alert($(this).attr('value'));})); return data; }Change this paragraph to this
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" ){ // Because json data will be wrapped by <pre> tag, there is a problem. Now add the following code, // update by hzy var reg = /<pre.+?>(.+)<//pre>/g; var result = data.match(reg); result = RegExp.$1; // update end data = $.parseJSON(result); // eval( "data = " + data ); // evaluate scripts within html } if ( type == "html" ) jQuery("<div>").html(data).evalScripts(); //alert($('param', data).each(function(){alert($(this).attr('value'));})); return data; }This will work normally.
Another case: ajaxFileUpload reports this error jQuery.handleError is not a function
Only versions before version 1.4.2 have handlerError method. The Jquery used in the example is 1.2. Solution:
In order to continue to use ajaxfileupload to upload our attachments, I had to copy the following code into the ajaxfileupload.js file in our project
handleError: function( s, xhr, status, e ) { // If a local callback was specified, fire it if ( s.error ) { s.error.call( s.context || s, xhr, status, e ); } // Fire the global callback if ( s.global ) { (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] ); } }For more exciting content, please refer to the special topics "Ajax Upload Technology Summary", "Javascript File Upload Operation Summary" and "JQuery Upload Operation Summary" for learning.
The above is the solution to the problem of reporting errors in ajaxupload.js. I hope it can help you solve the difficulties. I also hope that you will continue to pay attention to more exciting content on Wulin.com.