0

I want to fetch some data from my server and append a jQuery-Listview, which doesn't work. I get this error in the Chrome-Console when testing it:

Uncaught SyntaxError: Unexpected token i

The code I am using to consume the Json string coming from the server:

$.ajax({
        url: 'http://domain/GetArknr'
    })
    .done(function(data) {
        $.each(JSON.parse(data), function(idx, obj) {
            $('#listview').append('<li id="' + obj.ARKNR + '"><a href="#">' + obj.ARKNR + '</a></li>');
        })
        $('#listview').listview('refresh');
        alert(data)
    })
    .fail(function() {
        alert("Failed to fetch data");
    })

And the JSON:

[ { "arknr" : "0582",
    "id" : "0582"
  },
  { "arknr" : "0590",
    "id" : "0590"
  },
  { "arknr" : "0599",
    "id" : "0599"
  },
  { "arknr" : "1001",
    "id" : "1001"
  },
  { "arknr" : "1002",
    "id" : "1002"
  },
  { "arknr" : "1003",
    "id" : "1003"
  },
  { "arknr" : "1004",
    "id" : "1004"
  },
  { "arknr" : "1005",
    "id" : "1005"
  },
  { "arknr" : "1006",
    "id" : "1006"
  },
  { "arknr" : "1007",
    "id" : "1007"
  },
  { "arknr" : "1008",
    "id" : "1008"
  },
  { "arknr" : "1009",
    "id" : "1009"
  },
  { "arknr" : "1010",
    "id" : "1010"
  },
  { "arknr" : "1011",
    "id" : "1011"
  },
  { "arknr" : "1012",
    "id" : "1012"
  },
  { "arknr" : "1013",
    "id" : "1013"
  },
  { "arknr" : "1014",
    "id" : "1014"
  },
  { "arknr" : "1016",
    "id" : "1016"
  },
  { "arknr" : "1017",
    "id" : "1017"
  },
  { "arknr" : "1018",
    "id" : "1018"
  },
  { "arknr" : "1019",
    "id" : "1019"
  },
  { "arknr" : "1020",
    "id" : "1020"
  },
  { "arknr" : "1021",
    "id" : "1021"
  },
  { "arknr" : "1022",
    "id" : "1022"
  },
  { "arknr" : "1023",
    "id" : "1023"
  },
  { "arknr" : "1024",
    "id" : "1024"
  },
  { "arknr" : "1025",
    "id" : "1025"
  },
  { "arknr" : "1026",
    "id" : "1026"
  },
  { "arknr" : "1027",
    "id" : "1027"
  },
  { "arknr" : "1029",
    "id" : "1029"
  },
  { "arknr" : "1030",
    "id" : "1030"
  },
  { "arknr" : "1031",
    "id" : "1031"
  },
  { "arknr" : "1032",
    "id" : "1032"
  },
  { "arknr" : "1033",
    "id" : "1033"
  },
  { "arknr" : "1035",
    "id" : "1035"
  },
  { "arknr" : "1036",
    "id" : "1036"
  },
  { "arknr" : "1037",
    "id" : "1037"
  },
  { "arknr" : "1038",
    "id" : "1038"
  },
  { "arknr" : "1040",
    "id" : "1040"
  },
  { "arknr" : "1041",
    "id" : "1041"
  },
  { "arknr" : "1042",
    "id" : "1042"
  },
  { "arknr" : "1043",
    "id" : "1043"
  },
  { "arknr" : "1044",
    "id" : "1044"
  },
  { "arknr" : "1046",
    "id" : "1046"
  },
  { "arknr" : "1047",
    "id" : "1047"
  },
  { "arknr" : "1048",
    "id" : "1048"
  },
  { "arknr" : "1049",
    "id" : "1049"
  },
  { "arknr" : "1050",
    "id" : "1050"
  }
]

On serverside I am using Spring 4.1.2. The response is listed in Chrome as 'xhr' - I couldn't figure out how to declare the response as application/json, I tried several different annotations to the method which handles the requests /GetArknr:

@RequestMapping(value = "/GetArknr")
public ModelAndView getArknr() {
    HashMap <String, Object> model = new HashMap();
    List <Map<String, Object>> data = dataProvider.getArkNr();

    model.put("table", data);
    return new ModelAndView("json/arknrListJson", model);
}
Jim
  • 3,476
  • 4
  • 23
  • 33
Tobias Kuess
  • 739
  • 2
  • 14
  • 33

2 Answers2

1

Typically in Spring REST services you don't want a view, instead you want to use @ResponseBody and to specify the produced type is "application/json"

@RequestMapping(value = "/GetArknr", produces = "application/json")
@ResponseBody
public List<Map<String, Object>> getArknr() {
    HashMap <String, Object> model = new HashMap();
    List <Map<String, Object>> data = dataProvider.getArkNr();

    return data;
}

Then in your ajax call you can do something like:

$.ajax({
  cache : false,
  type : 'GET',
  url: 'http://domain/GetArknr',
  dataType : 'json',
  success : function(data) {
     if (data == null) {
         //error handling
     }
     $.each(data['something'], function(idx, obj) {
        $('#listview').append('<li id="' + obj.ARKNR + '"><a href="#">' + obj.ARKNR + '</a></li>');
    })
  }
}

You can also use this in your $.ajax call to request the right data format (set the Accept header to "application/json") Add Accept header to JQuery AJAX GET (via JSONP) request

Community
  • 1
  • 1
Jim
  • 3,476
  • 4
  • 23
  • 33
0

.ajax data variable type is selected in an intelligent way by jquery , most probably you don't need to parse data into json as it is already a JavaScript object . Can you please tell us what console.log(data) after function(data){ show you ?

Antoine Eskaros
  • 836
  • 5
  • 22