2017-04-06 30 views
0

我的項目中有一個問題。IE SyntaxError:JSON解析Java中的''''

我們在Java的後端和在HTML和AngularJS(v1.4.6)一FrondEnd應用。

在frondend中,我們顯示一個表格,其中包含從JSON格式的Java中檢索的一些記錄。

在Java中,我們使用GSON轉換的目的是JSON格式,然後我們將發送到瀏覽器。 這裏是我的Java代碼:

public class doSomething { 
    public void caricaFile(HttpServletRequest request, HttpServletResponse response, FiltriParameters filtri) throws IOException 
    { 
     RetObj r = caricaFile(request, response, filtri, ...); 
     response.getWriter().println(Utils.json(r)); 
    } 
} 

public class Utils { 
    public final static String json(Object obj) { 
     return new GsonBuilder().serializeNulls().create().toJson(obj); 
    } 
} 

這是我們的角功能

$scope.uploadFile = function(files) { 
    if (files.length == 0) 
     return; 

    openLoader(); 
    var fd = new FormData(); 
    //Take the first selected file 
    fd.append("file", files[0]); 

    $http.post(urlUploader, fd, { 
     withCredentials: true, 
     headers: {'Content-Type': undefined }, 
     transformRequest: angular.identity 
    }) 
    .success(function(response){ 
     closeLoader(); 
     response = angular.fromJson(response); 
     // do something 
    }) 
    .error(
     function(response){ 
      closeLoader(); 
      alert(response); 
     }); 

    $("#file-upload").val(""); 
} 

在Chrome中一切正常,將JSON得到接收,之後解析和所有行的HTML表顯示。

在Internet Explorer中出現以下錯誤。

SyntaxError: Expected '}' 
    at uc (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:15:463) 
    at Zb (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:82:227) 
    at Anonymous function (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:83:141) 
    at m (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:7:320) 
    at cd (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:83:125) 
    at d (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:84:366) 
    at Anonymous function (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:118:324) 
    at n.prototype.$eval (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:132:441) 
    at n.prototype.$digest (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:129:455) 
    at n.prototype.$apply (http://127.0.0.1:8085/HERMESCS2/js/generics/1-4-6angular.min.js?vid=120:133:234) 

這裏使用JSON一個引擎收錄後端返回到前端/角:在error功能

IE控制檯「響應未定義」。 https://pastebin.com/zUiX3AtV

我們已經測試了它,這是一個有效的JSON

什麼建議嗎?

+0

的JSON似乎是有效的。 –

+0

它可能是一個簡單的語法錯誤在你的JS,檢查超越這個功能 – CrazyMac

回答

0

問題解決了

我在頭響應改變了content-type到:text/json不知何故Internet Explorer的這個,而鍍鉻/火狐沒有影響!

public class doSomething { 
    public void caricaFile(HttpServletRequest request, HttpServletResponse response, FiltriParameters filtri) throws IOException 
    { 
     RetObj r = caricaFile(request, response, filtri, ...); 
     response.setContentType("text/json"); 
     response.getWriter().println(Utils.json(r)); 
    } 
} 
+0

正確的是'application/json' – devnull69