1

I'm trying to create $http get request to fetch some json data generated by my web service, but it returns null error. However, the $http request works fine when I use this sample url instead (it returns json string too)

This is my angular code :

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

This is my web service code :

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

When I run the above code, it returns json string like this :

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

The console log returns this :

Error : null Status : 0

What is the meaning of the null error? Or is there anything wrong with how I return the json string?

justrandom
  • 403
  • 4
  • 12

2 Answers2

1

Try using JSON_STRINGIFY, this will convert your incoming data into String format.

console.log(JSON_STRINGIFY(response.data));

TO verify what data your web service is returning, you can always check it by hitting your web service via postman.

Nhan
  • 3,595
  • 6
  • 30
  • 38
abhi
  • 117
  • 13
  • I tried this `console.log("Error : " + JSON.stringify(response.data));` and it still returns Error : null – justrandom Dec 29 '16 at 07:28
  • First try hitting your url /web service via postman , its a chrome extension , if you are satisfied with the result it returns it means something is wrong with the angular code . Please verify it and let me know – abhi Dec 29 '16 at 07:30
  • try using @GET @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) – abhi Dec 29 '16 at 07:44
  • in your web service . This should resolve your concern – abhi Dec 29 '16 at 07:45
  • I used postman to call my web service url like you mentioned, and it returns this : `{"records": [{"Name": "Sara"},{"Name": "David"}]}` – justrandom Dec 29 '16 at 08:06
  • try adding @GET @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_‌​XML}) – abhi Dec 29 '16 at 08:07
  • I added it, @GET @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_‌​‌​XML}). Now the web service return 404 error. Seems like this one cant be used i guess? – justrandom Dec 29 '16 at 08:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131776/discussion-between-abhi-and-justrandom). – abhi Dec 29 '16 at 09:52
0

I managed to solve this by adding CORS (Access-Control-Allow-Origin) to the response header, based on another SO answer. There's no problem with my angular code, it's just that I need to modify my web service code to enable the CORS.

So I just modified the part where it returns data to become like this :

return Response.status(200).entity(outerObject.toString()).header("Access-Control-Allow-Origin", "*").build();
Community
  • 1
  • 1
justrandom
  • 403
  • 4
  • 12