代碼如下:
//上傳文件 $("#CompChange").click(function() { var params = $("#CompchangeTable").serialize(); var json0={'video.slogan':$('#Cbasic_score').val(),'video.videoKind':$("#Cextra_score").val(), 'video.videoName':$("#name").val()}; $.ajaxFileUpload({ type: "POST", url: "adminAction-upFile.action", data:json0,//要傳到后臺的參數,沒有可以不寫 secureuri : false,//是否啟用安全提交,默認為false fileElementId:['file1','file2'],//文件選擇框的id屬性 dataType: 'json',//服務器返回的格式 async : false, success: function(data){ alert("成功"); }, error: function (data, status, e){ alert("失敗"); } }); });
上傳后會報錯:
從報錯上看可能是多了一個"<"符號。經過大量的嘗試后,終于發現了不對勁的地方。我是把dataType的類型換成了context類型,并在success函數中打印出data。
結果是:
你回發現返回的數據中有<pre>標簽,問題終于找到了,原來是JSon格式,但是返回的格式明顯不是JSon格式,在網上查了一下才知道有時候后臺必須要則么做,所以只能找別的方法了,最后就在ajaxfileupload.js文件里發現了這個:
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("<p>").html(data).evalScripts(); return data; }
這就返回的值,返回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"){ ////////////以下為新增代碼/////////////// data = r.responseText; var start = data.indexOf(">"); if(start != -1) { var end = data.indexOf("<", start + 1); if(end != -1) { data = data.substring(start + 1, end); } } ///////////以上為新增代碼/////////////// eval("data = " + data); } // evaluate scripts within html if (type == "html") jQuery("<p>").html(data).evalScripts(); return data; }
我們把中間的截取出來就行了。
這就是我的解決方法,希望對其他人也有用。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com