2017-06-22 172 views
0

我正在使用springmvc和angularjs讀取數據並在網頁上顯示。 爲了從URL中讀取數據,我使用了JSoup API。無法從彈簧控制器返回字符串並在網頁上顯示

在將從URL讀取的內容顯示回網頁時遇到錯誤。任何建議都會有所幫助。

代碼:

HTML:

<div ng-controller="readContentController"> 
           <div> 
            {{content}} 
           </div> 
          </div> 

js代碼:

//controller 

myApp.controller('readContentController', function ($rootScope, $scope, $sce, ReportService, $window) { 
    $scope.content = {}; 
    MyService.getDataFromURL().then(
     function (response) { 
      console.log("response :" + response); 
      $scope.content = response; 
      }, 
     function (errResponse) { 
      $rootScope.showError("error :" + errResponse); 
     }); 
}); 
//service call 
myServiceFactory.getDataFromURL = function(){ 
     var deferred = $q.defer(); 

     $http.get('/getHTMLDataFromURL') 
      .then(
       function (response) { 
        deferred.resolve(response.data); 
       }, 
       function(errResponse){ 
        console.error('Error while fetching data' + errResponse); 
        deferred.reject(errResponse); 
       } 
      ); 
     return deferred.promise; 
    } 

彈簧控制器:

@RequestMapping(value = "/getHTMLDataFromURL", method = RequestMethod.GET) 
    public 
    @ResponseBody String getHtmlStrigContent() throws Exception { 
     Document doc = Jsoup.connect("https://xyz.abc.com/myFile.pdf").get(); 
     String htmlString = doc.toString(); 
     System.out.println("asD" + htmlString); 
     return htmlString; 
} 

錯誤消息:

SyntaxError: Unexpected token < in JSON at position 0 
    at JSON.parse (<anonymous>) 
    at Ec (angular.min.js:16) 
    at ic (angular.min.js:93) 
    at angular.min.js:93 
    at q (angular.min.js:7) 
    at od (angular.min.js:93) 
    at f (angular.min.js:95) 
    at angular.min.js:132 
    at m.$eval (angular.min.js:146) 
    at m.$digest (angular.min.js:143) 
(anonymous) @ angular.min.js:119 
myService.js:158 Error while fetching dataSyntaxError: Unexpected token < in JSON at position 0 

我試圖從另一個網頁讀取內容並顯示在我們的應用程序網頁中。

回答

0

它看起來像Angular認爲響應應該是JSON而不是HTML。在您的@RequestMapping註釋中,請嘗試添加produces="text/plain"produces="text/html"

但是,您還有另一個問題:默認情況下,由於明顯的安全問題,Angular不會將變量的內容顯示爲HTML。您需要明確告訴Angular,您可以以這種方式使用它之前先信任這些內容。 See the documentation for $sce舉例說明如何做到這一點。

+0

它正在工作,但我想顯示html的輸出,我的意思是我從URL讀取html內容,當我在瀏覽器中打開它時,它會顯示網頁(當我們點擊URL時看到的相同頁面) 。目前它顯示爲字符串,我可以將響應顯示爲網頁而不是html的字符串表示。任何建議將是有益的。謝謝。 – sss

+0

@sss:見我的第二段。 :) –

+0

感謝您輸入@Alvin Thompson.It的工作。 – sss

0

這看起來像,js試圖解析JSON格式的HTML響應。

從AngularJS $ HTTP Documentation

所有請求的缺省頭,是

$httpProvider.defaults.headers.common (headers that are common for all requests): 
Accept: application/json, text/plain, */* 

默認的轉換是

If the data property of the request configuration object contains an object, serialize it into JSON format. 

可以與具有內容類型覆蓋默認在您的請求中設置標題。

var req = { 
method: 'GET', 
url: '/getHTMLDataFromURL', 
headers: { 
    'Content-Type': 'text/plain', 
    'Accept': 'text/plain' 
} 
} 

$http(req).then(function(){...}, function(){...}); 
+0

它刪除response.data時顯示相同的錯誤,並嘗試迴應 – sss

+0

編輯我的迴應,從AngularJS $ http文檔,內容類型的默認標題是:$ httpProvider.defaults.headers.common(常見的標頭對於所有請求): 接受:application/json,text/plain,*/*,您必須將其更改爲僅接受文本。 – abstractnature

+0

謝謝,現在在網頁上顯示爲字符串。我如何將返回的響應顯示爲網頁(因爲響應是html內容)。 – sss