我正在使用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
我試圖從另一個網頁讀取內容並顯示在我們的應用程序網頁中。
它正在工作,但我想顯示html的輸出,我的意思是我從URL讀取html內容,當我在瀏覽器中打開它時,它會顯示網頁(當我們點擊URL時看到的相同頁面) 。目前它顯示爲字符串,我可以將響應顯示爲網頁而不是html的字符串表示。任何建議將是有益的。謝謝。 – sss
@sss:見我的第二段。 :) –
感謝您輸入@Alvin Thompson.It的工作。 – sss