ajaxFileUpload插件,C#返回Json數(shù)據(jù)報錯問題的解決方案
報錯信息一:jQuery.handleError is not a function
上傳圖片的時候,通過F12,查看到這個錯誤。
解決方案:
jquery版本問題,handlerError只在jquery-1.4.2之前的版本中存在,jquery-1.4.2之后的版本中都沒有這個函數(shù)了。通過添加下面代碼,解決錯誤。
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]);
}
},
示例:
報錯信息二:SyntaxError:unexpected token <
解決了上述錯誤以后,出現(xiàn)了新的錯誤:
解決方案:
幾經(jīng)周折,通過查看ajaxFileUpload.js的源碼,我發(fā)現(xiàn)下面這樣一段代碼:
這里可以看到,返回的數(shù)據(jù)類型。其中類型為json的時候,是直接用eval函數(shù)生成Json對象的,所以我猜測,這里轉(zhuǎn)對象的時候,報錯了。于是我在上面添加了一個alert,看一看未轉(zhuǎn)換之前的數(shù)據(jù)格式是什么樣的。
通過alert彈窗發(fā)現(xiàn),返回數(shù)據(jù)的格式如下:
此時就可以解釋通,為何轉(zhuǎn)換不了對象了。因為它已經(jīng)不是一個正確的Json格式數(shù)據(jù)了,外面包了一層<pre>標簽,導致eval生成Json對象的時候解析失敗。
解決的思路為去掉前后<pre>標簽,使data變成正確的Json格式數(shù)據(jù),然后再用eval函數(shù)完成Json對象的生成。
Js代碼方式一(紅色標記為去掉<pre>標簽):
function FileUpload() {
$.ajaxFileUpload({
url: '/Common/Image',
fileElementId: 'upload_img',
dataType: 'content',//此處寫content,是因為想讓ajaxFileUpload直接return data數(shù)據(jù),即帶<pre>標簽的數(shù)據(jù)。
success: function(data) {
var reg = /<pre.+?>(.+)<\/pre>/g;
var result = data.match(reg);
data = RegExp.$1;
var obj = eval("data=" + data); //轉(zhuǎn)josn
if (obj.error == 1) {
$("#images_src").attr("src", obj.msg);
$("#img_path").val(obj.msg);
} else {
alert("失敗");
}
},
error: function(data, status, e) {
alert(e);
}
});
return false;
}
Js代碼方式二(通過修改ajaxFileUpload內(nèi)部方法,紅色標記為去掉<pre>標簽):
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();
//此處是去掉<pre>標簽的代碼,新添加一種類型,前臺js的dataType類型寫content,即dataType: 'content'
if (type == "content") {
var reg = /<pre.+?>(.+)<\/pre>/g;
var result = data.match(reg);
data = RegExp.$1;
eval("data = " + data);
}
return data;
}
以上是在QQ瀏覽器進行的,當我使用火狐瀏覽器的時候,又出現(xiàn)了新的錯誤。
報錯信息三:ReferenceError: $ is not defined
解決方案:
沒有看懂錯誤信息,通過用最開始的方法,我在ajaxFileUpload.js代碼中寫了alert,彈一下data數(shù)據(jù),發(fā)現(xiàn)數(shù)據(jù)格式變成這樣:
<pre>標簽的內(nèi)容發(fā)生了變化,所以我改變了思路,通過indexOf定位< >,然后只取標簽中間的內(nèi)容。代碼如下:
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();
//此處是去掉<pre>標簽的代碼
if (type == "content") {
//------以下是修改的代碼------
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);
}
return data;
}
后來想了想,可以用過大括號來篩選,這樣更準確一些吧。
var start = data.indexOf("{");
if (start != -1) {
var end = data.indexOf("}", start + 1);
if (end != -1) {
data = data.substring(start + 1, end);
}
}
總結(jié)
以上所述是小編給大家介紹的ajaxFileUpload插件,C#返回Json數(shù)據(jù)報錯問題的解決方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用C#獲取遠程圖片 Form用戶名與密碼Authorization認證的實現(xiàn)
本篇文章介紹了,使用C#獲取遠程圖片 Form用戶名與密碼Authorization認證的實現(xiàn)。需要的朋友參考下2013-04-04
C# 使用multipart form-data方式post數(shù)據(jù)到服務器
這篇文章主要介紹了C# 使用multipart form-data方式post數(shù)據(jù)到服務器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

