I am using Ajax&Jquery to handle the JSON data( In List) comes from the server, however, the client end cannot accept the JSON data and constantly popped up the error method.
I have implemented a renderList() method to parse the List ,however it still doesn't work.
The following is my Javascript and Java code.
Can someone help me out ? Thanks in advance.
JavaScript:
var serviceURL = "http://localhost:8080/TransportationNetwork/rest/paths";
$('#findPaths').click(function() {
getPaths();
});
function getPaths() {
console.log('display paths');
$.ajax({
type:'GET',
url: serviceURL,
dataType:'json', // data type get back from server
data:'st=' + dataToServer(), //data sent to server
success: renderList,
error: function(jqXHR, textStatus, errorThrown){
alert('Path Finder: ' + textStatus);
}
});
}
function dataToServer() {
var array = "";
str1 = $('#source').val();
str2 = $('#target').val();
array = str1 + "," + str2;
return array;
}
function renderList(data) {
var list = data == null ? [] : (data instanceof Array ? data : [data]);
$('#PathList li').remove();
$.each(list, function(index, path) {
$('#PathList').append('<li>'+ path.source + ' -> ' + path.target + ': ' + path.weight + '</li>');
});
}
Java: In Resource File:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<DirectedEdge> pathsInfo(@QueryParam("st") String st) {
System.out.println("Searching paths : " + st);
return pathDao.getEdgeList(st);
}
The returned DirectedEdge Objects:
public class DirectedEdge {
private int source;
private int target;
private double weight;
public DirectedEdge() {
}
public DirectedEdge(int v, int w, double weight) {
super();
this.source = v;
this.target = w;
this.weight = weight;
}
.... Other code ...
}