AngularJS $ http获取返回空状态0

公正

我正在尝试创建$ http get请求以获取由我的Web服务生成的一些json数据,但它返回null错误。但是,当我使用此示例网址代替时,$ http请求可以正常工作(它也返回json字符串)

这是我的角度代码:

angular.module('ionicApp', ['ionic'])

.controller('ListCtrl', function ($scope, $http) {
  $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

  $http.get("http://localhost:8080/InventoryCtrl_Service/webresources/IVC_Service/GetUserList")
   .then(function(response) {
       console.log("success ");
   }, 
   function(response) {
       console.log("Error : " + response.data + " Status : " + response.status);
   }
});

这是我的网络服务代码:

@GET
@Path("/GetUserList")
@Produces("application/json")
public Response GetUserList() throws SQLException {

  net.sf.json.JSONObject json = new net.sf.json.JSONObject();       
  JSONObject obj1 = new JSONObject();    
  JSONObject obj2 = new JSONObject();  
  JSONObject outerObject = new JSONObject();   
  JSONArray arr = new JSONArray();        

  obj1.put("Name", "Sara");
  obj2.put("Name","David");              
  arr.add(obj1);
  arr.add(obj2);

  outerObject.put("records", arr);

  return Response.status(200).entity(outerObject.toString()).build();
}

当我运行上面的代码时,它将返回json字符串,如下所示:

{"records":[{"Name":"Sara"},{"Name":"David"}]}

控制台日志返回以下内容:

Error : null Status : 0

空错误是什么意思?还是我返回json字符串有什么问题吗?

公正

我设法通过基于另一个SO答案在响应标头中添加CORS(Access-Control-Allow-Origin)来解决此问题我的角度代码没有问题,只是我需要修改我的Web服务代码以启用CORS。

所以我只是修改了它返回数据的部分,使其像这样:

return Response.status(200).entity(outerObject.toString()).header("Access-Control-Allow-Origin", "*").build();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章