【发布时间】:2017-05-13 11:33:04
【问题描述】:
我正在尝试创建 $http get 请求以获取我的 Web 服务生成的一些 json 数据,但它返回 null 错误。但是,当我改用 sample url 时,$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
null 错误是什么意思?或者我如何返回 json 字符串有什么问题吗?
【问题讨论】:
-
尝试
POSTMAN验证响应。 -
你的后端有 Spring 吗?
-
@RohitJindal 我使用了
POSTMAN,它返回{"records": [{"Name": "Sara"},{"Name": "David"}]}。显示的状态为 200 OK。 -
太好了。那么,您能否将第二个函数参数从
response更改为error并将这个"Error : " + error.data + " Status : " + error.status放在console.log 中。 -
@RohitJindal 试过了。控制台日志仍然显示相同的错误
Error : null Status : 0
标签: java angularjs arrays json