2017-06-15 60 views
0

我正在使用Restangular來使用REST API,即使我可以在開發人員控制檯上看到XHR請求已成功創建,它總是會給我這個錯誤。儘管請求成功,但爲什麼會出現錯誤

錯誤

{ 
    "data":null, 
    "status":-1, 
    "config":{ 
     "method":"GET", 
     "transformRequest":[null], 
     "transformResponse":[null], 
     "jsonpCallbackParam":"callback", 
     "headers":{"Accept":"application/json, text/plain, */*"}, 
     "url":"http://localhost:8080/profile/v1/support/tickets"}, 
     "statusText":"" 
} 

Restangular API調用

angular.module('adf-widget-tickets-module-service',['restangular']) 
    .service('ticketCRUDService', function ($rootScope, Restangular, $http) { 
    Restangular.setBaseUrl('http://localhost:8080/profile/v1/support'); 
    this.readTickets = function() { 
     Restangular.all('tickets').getList().then(function (response) { 
      var test = response.json; 
      console.log(test); 
      return test; 
     },function (err) { 
      console.error(JSON.stringify(err)); 
     },function() { 
      console.log("loading......"); 
     }) 
    }; 
} 

你能告訴我我在做什麼錯在這裏?

更新

這裏是我的REST端點

@GET 
@Path("tickets") 
@Produces("application/json") 
public Response getAllTickets(){ 
    ArrayList<Ticket> allTickets = new ArrayList<>(); 
    try { 
     allTickets = elasticAPI.getAllTickets(); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } 
    return Response.ok(gson.toJson(allTickets), MediaType.APPLICATION_JSON_TYPE).build(); 
} 

回答

1

爲什麼你的終點返回-1狀態代碼的代碼?只有

角之間做出決議:和200299

/* 
* A response status code between 200 and 299 is considered a success status and will result in 
* the success callback being called. Any response status code outside of that range is 
* considered an error status and will result in the error callback being called. 
* Also, status codes less than -1 are normalized to zero. -1 usually means the request was 
* aborted, e.g. using a `config.timeout`. 
* Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning 
* that the outcome (success or error) will be determined by the final response status code. 
*/ 

https://github.com/angular/angular.js/blob/master/src/ng/http.js#L457

+0

這是我的。當我使用REST客戶端測試端點時,它會給出響應代碼200.我還可以看到正在發出的請求並在開發者控制檯上獲得200響應。這可能是一個CORS問題嗎? –

0

正如我猜這個問題是不是在我的客戶端代碼。這是因爲CORS導致請求被服務器拒絕。爲CORS支持設置適當的響應頭解決了這個問題。

相關問題