2017-06-15 9 views
0

Ich benutze Restangular, um eine REST-API zu konsumieren, und es gibt mir immer diesen Fehler, obwohl ich die XHR-Anforderung erfolgreich auf der Entwicklerkonsole sehen kann.Warum gibt Restangular einen Fehler, obwohl die Anfrage erfolgreich ist

Fehler

{ 
    "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-Aufruf

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......"); 
     }) 
    }; 
} 

Könnten Sie mir bitte sagen, was mache ich falsch hier?

aktualisieren

Hier ist der Code für meine REST-Endpunkt

@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(); 
} 

Antwort

1

, warum Ihr Endpunkt einen -1 Statuscode zurückgibt?

Angular Entschlüssen nur zwischen: 200 und 299

/* 
* 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

Es ist weired. Wenn ich den Endpunkt mit einem REST-Client teste, gibt es den Antwortcode 200. Ich kann auch sehen, dass die Anfrage gestellt wird und eine Antwort von 200 auf der Entwicklerkonsole erhalten wird. Könnte das ein CORS-Problem sein? –

0

Wie vermutete ich das Problem nicht auf meinem Client-Seite Code war. Aus diesem Grund wurde die Anfrage aufgrund von CORS vom Server abgelehnt. Durch das Setzen passender Response-Header für die CORS-Unterstützung wurde das Problem behoben.

Verwandte Themen