簡體   English   中英

AngularJS沒有得到API響應

[英]AngularJS not getting API Response

我正在嘗試使用AngularJS和在OSGi環境(KARAF)中運行的CXF-JAXRS庫,來嘗試運行一個簡單的API響應。

日志顯示AngularJS已正確連接到REST服務,並且已正確響應200狀態代碼。 但是,在AngularJS中,不會檢索回發的信息。

僅供參考:我必須從所有鏈接中刪除“ http://”,因為StackExchange告訴我我沒有足夠的聲譽來包含它們。 但是它們在實際的代碼/日志中。

我正在連接的REST方法

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(){
    String testResponse = "Success.";
    Response responseString = Response
            .status(200)
            .entity(testResponse)
            .build();
    return responseString;
}

AngularJS控制器

        app.controller('HttpController', function($scope, $http){
        $http({
            method: 'GET',
            url: 'localhost:8181/cxf/strsoftware/test'
        }).then(function successCallback(response){
                $scope.testString = response.data;
        }, function errorCallback(response){
                $scope.testString = response;
        });
        $scope.validationString = "Controller is functional.";
    });

AngularJS顯示代碼

<div ng-controller="HttpController">
The response is {{ testString }}<br>
{{validationString}}

在AngularJS HTML頁面上顯示

連接時顯示KARAF日志

2017-01-10 11:42:30,133 | 信息| qtp84694963-897 | LoggingInInterceptor | 107-org.apache.cxf.cxf-core-3.2.0。 入站郵件

ID:20地址:localhost:8181 / cxf / strsoftware / test HTTP方法:GET內容類型:

標頭:{Accept = [應用程序/ json,文本/純文本, / ],accept-encoding = [gzip,deflate,sdch,br],Accept-Language = [en-US,en; q = 0.8],connection = [ keep-alive],Content-Type = [null],Host = [localhost:8181],Origin = [localhost:63343],Referer = [localhost:63343 / STRFrontEnd / StartScreen.html?_ijt = qsu1c8a1qskj0def9ho1rml4hv],用戶代理= [Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 55.0.2883.87 Safari / 537.36]}

2017-01-10 11:42:30,135 | 信息| qtp84694963-897 | LoggingOutInterceptor | 107-org.apache.cxf.cxf-core-3.2.0。 出站郵件

ID:20響應代碼:200內容類型:application / json標頭:{Content-Type = [application / json],Date = [Tue,2017年1月10日16:42:30 GMT]}

有效負載:成功。

更新

通過在Chrome(Ctrl + Shift + I)上打開“檢查”頁面並單擊“控制台”,我們發現了以下錯誤:

angular.js:11756XMLHttpRequest無法加載localhost:8181 / cxf / strsoftware / test。 跨源請求僅支持以下協議方案:http,數據,chrome,chrome擴展名,https,chrome-extension-resource。

這是服務器端錯誤,因此我們目前正在服務器端中實施CORS,以查看是否可以解決問題。 找到后將發布解決方案。

您正在.then()中調用匿名函數。 所以應該是這樣的:

$http({
        method: 'GET',
        url: 'localhost:8181/cxf/strsoftware/test'
    }).then(function(response){
            $scope.testString = response.data;
    }, function(response){
            $scope.testString = response;
    });

如果無法正常工作,請嘗試使用此命令params:{} 替換data ::

        $http({
            method: 'GET',
            url: 'localhost:8181/cxf/strsoftware/test',

            // Just to be sure! maybe you don't need it
            dataType: 'json',
            headers: {
                "Content-Type": "application/json"
            },

            //To be sure Angular does NOT remove the content-type header.
            data: '',
        }).then(function successCallback(response) {
            $scope.testString = response.data;
        }, function errorCallback(response) {
            $scope.testString = response;
        });

編輯

查看此答案下面的評論,以了解Shadmehr在哪里解決了問題。 解決方案是在應用程序的服務器端部分實現CORS篩選器。 嘗試訪問API時,瀏覽器中出現跨區域錯誤。

不知道您的js代碼是否正確。 我還嘗試了angular js,我的代碼如下所示:

$http.get("/cxf/strsoftware/test")
  .success(function (response) {$scope.testString = response.data;});

有關我的完整代碼,請參見blueprint-cdi-example

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM