簡體   English   中英

Angular $ http.get總是在消耗本地Restful Service時出錯

[英]Angular $http.get always get ERROR consuming local Restful Service

我遇到一個錯誤,並且感到困惑,我使用Jersey創建了一個簡單的Java EE 7項目。

我將在我的休息服務處返回該課程:

@XmlRootElement
public class LocationDTOx {

    private Long id;        
    private String tittle;    
    private String description;        
    private Long parent;

    //Getter and setters...

在我的服務班里,我有:

@Path("/location")
public class LocationService {

    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findlocation")
    public LocationDTOx findLocation() {
        System.out.println("findlocation");
        try {
            LocationDTOx x = new LocationDTOx();
            x.setDescription("Description");
            x.setId(0l);
            x.setParent(null);
            x.setTittle("Tittle ...");
            return x;
        } catch (Exception ex) {
            Logger.getLogger(LocationService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }    

}

如果將其放在瀏覽器中,我100%肯定其余的都可以正常工作:

http:// localhost:8080 / BIReportL-war / rest / location / findlocation

我得到這個Json字符串:

{“ description”:“ Description”,“ id”:0,“ tittle”:“ Title ...”}

關鍵是在我的角度代碼中,我正在從角度調用其余服務的代碼正在執行,但這只是給我錯誤部分:

app.controller('questionsController', function ($scope, $http) {
    //var url = "http://localhost:8080/BIReportL-war/rest/location/findlocation";
    //var url = "http://www.w3schools.com/angular/customers.php";
    var url = "http://127.0.0.1:8080/BIReportL-war/json.json";
    $http.get(url)
        .success(
            function (data, status, headers, config) {
                alert("success");   
            })
        .error(function(data, status, headers) {
          alert('Repos status ' + status + ' --- headers : ' + headers);
        })
        .finally(
            function() {
        });
});

我在注釋中有另一個本地URL,可以訪問該虛擬json文件,該瀏覽器可以訪問該本地json,並且我也得到相同的結果錯誤,這很奇怪,我嘗試使用此其余公共json文件:

http://www.w3schools.com/angular/customers.php

我得到了成功! 我不知道為什么,我在做什么或有什么錯,我的意思是當我嘗試使用本地休息服務時,我看到它在日志中被調用,這是事實,但是有角度的客戶端正在獲取變成錯誤。

在此先感謝您的幫助 !

我正在使用:* Glassfish V4 * Angular

好吧,關於CORS問題,我只是按照以下說明休息,所以這里是解決方案:

@Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findlocation")
    public Response findLocation() {
        System.out.println("findlocation");
        try {
            LocationDTOx x = new LocationDTOx();
            x.setDescription("Description");
            x.setId(0l);
            x.setParent(null);
            x.setTittle("Tittle ...");

            return Response.ok()
                    .entity(x)
                    .header("Access-Control-Allow-Origin", "*")
                    .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
                    .build();
        } catch (Exception ex) {
            Logger.getLogger(LocationService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }   

如果AngularJS正在訪問您的本地REST API,那么您實際上是在瀏覽器中的其他端口上運行它,那么根據CORS的規則,它被視為不同的來源(單獨的端口表示單獨的來源)。

如果兩個頁面的協議,端口(如果指定了一個)和主機相同,則兩個頁面的來源相同。

對於您的Access-Control-Allow-Origin響應標頭,請通過*將其設置為all,或者顯式指定備用端口。 這與您的瀏覽器(和AngularJS)嘗試按照規則進行播放有關,您可以在MDN的同一原始策略頁面上找到這些規則。

當您直接在瀏覽器中加載資源時,這些“規則”不適用,因為源(瀏覽器從中加載的頁面)是相同的端口,而您只是在該源(加端口)加載資源。

[編輯]

CORS標准包括遵守某些響應標頭,例如Access-Control-Allow-Origin和Access-Control-Allow-Methods。

參考文獻:

[/編輯]

您的Jersey服務正在使用GET(@GET),而Angular客戶端正在使用POST($ http.post(url))。

將Angular代碼更改為$ http.get,一切順利。

您的http://www.w3schools.com/angular/customers.php示例正在運行,因為它同時響應POST和GET,但是對於您的方案GET顯然是正確的HTTP動詞。

您是否嘗試使用相對網址? var url = "/BIReportL-war/json.json";

您可以在此處發布整個錯誤嗎?

我同意@pankajparkar,這可能是CORS問題。

(很抱歉張貼此“答案”,我沒有足夠的意見要發表)

暫無
暫無

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

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