2016-08-16 25 views
-1

我有一點問題。我正在研究angularjs單個Web應用程序頁面,我正在嘗試在角度模塊中添加SockJS。AngularJS意外的標記<位於ng視圖中位置0的JSON中

這是我的主頁。

<html ng-app="myApp"> 
    <head> 
     <title>Timeline Page</title> 
     <meta charset="utf-8"> 
     <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" /> 
     <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" /> 
     <link href="css/bootstrap-switch.min.css" rel="stylesheet" type="text/css" /> 
     <link href="css/components.min.css" rel="stylesheet" id="style_components" type="text/css" /> 
     <link href="css/plugins-md.min.css" rel="stylesheet" type="text/css" /> 
     <link href="css/layout.min.css" rel="stylesheet" type="text/css" /> 
     <link href="css/light.min.css" rel="stylesheet" type="text/css" id="style_color" /> 
     <link href="css/custom.min.css" rel="stylesheet" type="text/css" /> 
    </head> 
    <body class="page-container-bg-solid page-md"> 
     <div ng-view></div> 
    </body> 
    <script type="text/javascript" src="js/lib/angular.min.js"></script> 
    <script type="text/javascript" src="js/lib/angular-route.min.js"></script> 
    <script type="text/javascript" src="js/app.js"></script> 
    <script type="text/javascript" src="js/lib/sockjs-0.3.4.js"></script> 
    <script type="text/javascript" src="js/lib/stomp.js"></script> 
</html> 

這是js代碼。

var myApp = angular.module('myApp', ['ngRoute', 'myApp.controllers', 'myApp.services', function() { 
     var socket = new SockJS('/hello'); 
     stompClient2 = Stomp.over(socket); 
     stompClient2.connect({}, function (frame) { 
      con = true; 
      console.log('Connected: ' + frame); 
      stompClient2.subscribe('/topic/greetings', function (greeting) { 
       showGreeting(JSON.parse(greeting.body).content); 
      }); 
     }); 
    }]); 

當我嘗試這一點,我有這樣的問題:

Uncaught SyntaxError: Unexpected token < in JSON at position 0 

我有很多頁,我想開一個插座,當我打開網頁,但它不工作。任何人都可以幫助我?

編輯 Greeting.class

public class Greeting { 

    private String content; 

    public Greeting(String content) { 
     this.content = content; 
    } 

    public String getContent() { 
     return content; 
    } 

} 

GreetingController.class

@Controller 
public class GreetingController { 

    @MessageMapping("/hello") 
    @SendTo("/topic/greetings") 
    public Greeting greeting(HelloMessage message) throws Exception { 
     //Thread.sleep(1000); // simulated delay 
     return new Greeting("Hello, " + message.getName() + "!"); 
    } 

} 
+1

你能記錄'greeting'的內容嗎? –

+0

問題解決。這只是安全問題。 –

回答

-1

您必須具有無效JSON響應。刪除JSON.parse應該有助於避免此錯誤。不過,你應該只是在服務器端工作。您可以使用JSON Lint來驗證響應JSON。

var myApp = angular.module('myApp', ['ngRoute', 'myApp.controllers', 'myApp.services', function() { 
     var socket = new SockJS('/hello'); 
     stompClient2 = Stomp.over(socket); 
     stompClient2.connect({}, function (frame) { 
      con = true; 
      console.log('Connected: ' + frame); 
      stompClient2.subscribe('/topic/greetings', function (greeting) { 
       showGreeting(greeting.body); 
      }); 
     }); 
    }]); 
2

它好像無論是從greeting.body回來是不是一個有效 JSON字符串。

鑑於你的錯誤在位置0有一個<我會假設HTML從你的API而不是意外返回。

+0

我在一個頁面的不同項目中嘗試了相同的代碼,它正在工作,但是當我嘗試這樣的單個網頁應用程序時。它不工作。我不認爲是這樣。 –

+0

@Gürsel註銷「greeting.body」中的內容...... – Neal

+0

我添加了這些類。 –

相關問題