2017-06-15 64 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支持设置适当的响应头解决了这个问题。

相关问题