2017-04-07 37 views
-2

我有了一個REST功能是這樣的:如何從services.js調用REST函數?

@Override 
public String getNameForMW() { 
    String MW_Url = props.getProperty("MW_Url"); //<<this returns a String 
    return MW_Url.split("/")[3].toString(); 
} 

然後,我有一個接口:

@GET 
@Produces(MediaType.TEXT_PLAIN) 
@Path("/getNameForMW") 
public String getNameForMW(); 

然後我有services.js文件,其中我廠的所在地,並有這一行:

var pathForMW = $http.get(path + 'getNameForMW'); 

,但結果不是一個字符串,但像這樣的對象: enter image description here

可以幫我嗎?

我需要它是一個簡單的字符串,我蘆葦從屬性文件

+0

這是......角度...?你知道'$ http.get'不會立即返回*結果,但你必須註冊一個*異步回調* ...? – deceze

+0

不......我應該怎麼做? – newbie

+0

https://docs.angularjs.org/api/ng/service/$http – deceze

回答

1

THX到deceze,我想出了這個

var pathForMW = $http.get(path + 'getNameForMW').then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    pathForMW = response.data; 

    } 
); 

是解決我的問題.... THX再次

0

這是一個使用$ http進行的異步調用。所以,一個承諾返回。您需要使用.then,以便您可以對返回的響應執行操作。

$http.get(path + 'getNameForMW').then(function 
           successCallback(response) { 
       // this callback will be called asynchronously 
       // when the response is available 
       // you can do something with the response here 
       // like saving it to a variable 

}, function errorCallback(response) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
});