I was trying to send String data using Ajax and then send back the JSON data from the server using the Java code. But every time when I try to run the javascript it alerts "Path Finder : error".
And when I try to put a breakpoint in the "return" row in java, it just doesn't work, so I guess it is has a problem on the request stuff.
But when I use POSTMAN to test the data, it works! (shown as the picture).
Can anyone help me with this problem? Thousand Thanks !!

JavaScript:targetpaths.js
var serviceURL = "http://localhost:8080/TransportationNetwork/rest/paths";
$('#findPaths').click(function() {
getPaths();
});
function getPaths() {
console.log('display paths');
$.ajax({
type:'POST',
contentType:'application/json', //data type sent to server
url: serviceURL,
dataType:"json", // data type get back from server
data: dataToServer(), //data sent to server
success: function(data, textStatus, jqXHR){
alert('Path created successfully');
},
error: function(jqXHR, textStatus, errorThrown){
alert('Path Finder : ' + textStatus);
}
});
}
function dataToServer() {
var array = "";
var str1 = $('#source').val();
var str2 = $('#target').val();
array = str1 + "," + str2;
return array;
}
<!DOCTYPE html>
The HTML:
<head>
<title>Path Finder</title>
</head>
<body>
<div style="padding-left:100px;font-family: monospace;">
<h2>Path Finder</h2>
<div style="width: 200px; text-align: left;">
<div style="padding:10px;">
Source Node: <input id="source" name="source" />
</div>
<div style="padding:10px;">
Target Node: <input id="target" name="target" />
</div>
<div style="padding:10px;text-align:center">
<button id="findPaths">Find Paths</button>
</div>
</div>
<ul id="paths"></ul>
</div>
<p id="demo"></p>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/targetpaths.js"></script>
Java Code:
@Path("/paths")
public class PathsResource {
PathDao pathDao;
public PathsResource() {
pathDao = new PathDao();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes("text/plain")
public List<DirectedEdge> pathsInfo(@QueryParam("st") String st) {
System.out.println("Searching paths : " + st);
return pathDao.getEdgeList(st);
}
UPDATA:
I changed above code to things below, It did invoke the "System.out.println("Searching paths : " + st);"
correctly.
However, the browser still send me the error alert, I do not know how to solve this problem, is that because it didn't return a correct JSON format? but why I can see the returned data is correct in POSTMAN??? HELP!!!!
JAVA:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<DirectedEdge> pathsInfo(@QueryParam("st") String st) {
System.out.println("Searching paths : " + st);
return pathDao.getEdgeList(st);
}
JAVASCRIPT:
var serviceURL = "http://localhost:8080/TransportationNetwork/rest/paths";
function dataToServer() {
var array = "";
str1 = $('#source').val();
str2 = $('#target').val();
array = str1 + "," + str2;
return array;
}
$('#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: function(data){
alert('Path created successfully');
},
error: function(jqXHR, textStatus, errorThrown){
alert('Path Finder : ' + textStatus);
}
});
}
dataToServer()returns? It looks like invalid JSON.dataToServer().