0

I have a $http get method which should invoke my local server. I am getting proper response from my server through postman. Following is my Json values:

[{"id":27885,"bslRef":acms.2016.04.00,"size":0,"isDefault":true,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}]

As "bslRef":acms.2016.04.00 which is the value I am interested in is not enclosed in a json tag [""].

That is why i am getting an parsing error. This is the error I am getting from jsonlint.

Error: Parse error on line 3:
...: 27885, "bslRef": acms .2016 .04 .00,
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Following is my angularjs controller code:

$http.get('/baseline/getBaseline?', {params: {
                       currProject: $scope.selectedProject}})
                   .success(function(data) {

                       $scope.baselines=JSON.stringify(data);
                   })
                   .error(function(data) {
                       alert("failure");
                   });

I have tried with parse() method and stringify() method but of no use.

My server side application is a spring-mvc rest application, which is giving me proper response, But in the said format.

Please some one help me either parse this through angular methods, or get me a json type response from my spring controller. TIA.

  • What type is `bslRef` in the server code? And also `$scope.baselines` is a string or an object? If it's an object no need to `JSON.stringify`. – Mihail Stancescu Mar 08 '16 at 10:10
  • $scope.baselines is an object. It has a format of [{"id":27885,"bslRef":acms.2016.04.00,"size":0,"isDefault":true,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}] – saroj mishra Mar 08 '16 at 10:22
  • bslref is an object type in the server code. – saroj mishra Mar 08 '16 at 10:23

2 Answers2

0

One of the solution is to transform the response object yourself using a function to handle the repsone when it returns...

This can be found at angular documentation.

$http({
 url: '...',
 method: 'GET',
 //here you get the unparserd response ...
 transformResponse: appendTransform($http.defaults.transformResponse,  

    //you data response comes here ..which you can handle customly
    function(data) {
      //perform modifications & get data
      $scope.baselines = doTransform(data);
    })
});

And then you can bind data to Html DOM as desired. This is for single get request.If you want to use it at all places , try to create a service or factory.

Thanks, HTH

damitj07
  • 2,689
  • 1
  • 21
  • 40
0

The issue got solved. Actually in my pojo class for Baseline I had added following Json annotation.

private long id;
@JsonSerialize(as = BaselineRef.class)
@JsonRawValue
private BaselineRef bslRef;
private int size;
@JsonProperty
private boolean isDefault = false;
private boolean isOpen = true;
private boolean isDaily = false;
private String baselineDate;
private BaselineRef parentRef;
private Collection<String> activities;

Hence while retrieving I was getting following types of response.

[{"id":27885,"bslRef":acms.2016.04.00,"size":0,"isDefault":true,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}].

Once i removed the annotation Now I am getting

 `[{"id":27885,"bslRef":{"bslName":"2016.04.00","projectName":"acms","overrides":false},"size":0,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}]` 

which is my desired response. Here i ensured that BaselineRef has an toString method. Here I dont have to parse in my angular code. Thanks for everyone help.