跳到主要內容

[JQuery] ajaxfileUpload.js return JSON format Error

在調整asp.net MVC上傳程式的時候,
發現actionresult
return JSON時候,會出現下面的錯誤
SyntaxError: Unexpected token <

仔細查了一下,才發現
Retrun回來的JSON,不是單純的JSON資料而已
還被加上了<pre ......的資料

所以回傳回來後javascript接到就認為格式不正確。

主要原因出在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("<div>").html(data).evalScripts();

        return data;
    }

只要將原本的
      if (type == "json")
            eval("data = " + data);
修改成
 if (type == "json"){
            if (data.indexOf("<") >= 0) {
                data = jQuery.parseJSON(jQuery(data).text());
            }
            else {
                eval("data = " + data);  /*Bug  fixed by under */
            }
         }
就可以解決這個問題。
此外,如果將Jquery更新到新的版本,
會因為新的Jquery沒有 handleError 的元件而使得ajaxfileupload.js出錯,
這時候可以在ajaxfileupload.js最後面加上的
 handleError: function(s, xhr, status, e) {
        // If a local callback was specified, fire it
        if (s.error) {
            s.error.call(s.context || window, xhr, status, e);
        }

        // Fire the global callback
        if (s.global) {
            (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
        }
    }
解決這個錯誤

在google有查到 不只是asp.net回傳 json有這樣的狀況,
有其他人是在FF(firefox)會遇到這樣的狀況,
都可以修改ajaxfileuplolad.js的JSON判斷來處理。

留言

這個網誌中的熱門文章

[WEB]連線 HTTPS 網站發生驗證失敗導致基礎連接已關閉

某支透過 WebClient 物件去呼叫第三方API的程式,突然有天無法使用 經過測試出現下列的錯誤 基礎連接已關閉: 傳送時發生未預期的錯誤。 InnerException : 驗證失敗,因為遠端群體已經關閉傳輸資料流。 原來是第三方的服務已經不支援 TLS 1.0 我方的程式是用.net Framework 4.0開發了 得強制讓webclient改用 TLS 1.1 或 TLS 1.2 感謝黑大提供解決方法 在程式中加入 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12  的設定就解決了這個問題 WebClient wc = new WebClient(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 參考資料:暗黑執行緒

[SQL] SQL依照你的排序條件 找出目前資料的前一筆與下一筆。 Find Pre and Next DataRows of current Datarow by your order condition

有時候需要用SQL找出前一筆跟後一筆資料 用SQL的TOP是沒有辦法做到 這個時候就可以這個語法 select * from ( SELECT TOP 1 * FROM [Article] where Poid {CurrentPoid} order by CreateDate ASC) t2 找出目前PK id前一個與後一個的資料(依照想要排序順序) 那如果指示想要一個資料行呈現的話 可以改用下面的SQL語法 讓這兩筆資料join在同一筆 select Pre.*,Nex.* from (SELECT TOP 1 * ,1 tID FROM [dbo].[Article] where Poid {CurrentPoid} order by CreateDate ASC) Nex on Pre.tID=Nex.tID

Win10電腦 強制開啟IE瀏覽器 by VB script

 用Notapad寫下以下的內容 With CreateObject("InternetExplorer.Application") .visible = True .Navigate [Url]           .Left = 0           .Top = 0           .Height = 1024           .Width = 1280 End With 然後存檔成 .vbs 檔案 之後點選檔案就會自動啟動IE 目前測試過在WIN10 版本能正常執行