I request json or xml data from the server.
I console log and for JSON I see json data and for XML i see #Document
How do I download this data to a file json file and xml respectivily?
Java
@RestController
public class RestCtrl {
@Autowired
JdbcTemplate jdbcTemplate;
@RequestMapping(value="getJSON", method = RequestMethod.GET , produces = {MediaType.APPLICATION_JSON_VALUE})
List<Map<String, Object>> getJSONData(@RequestParam String query) {
List<Map<String, Object>> data = jdbcTemplate.queryForList("SELECT * FROM TICKETS");
return data;
}
@RequestMapping(value="getXML", method = RequestMethod.GET , produces = {MediaType.APPLICATION_XML_VALUE})
List<Map<String, Object>> getXMLData(@RequestParam String query) {
List<Map<String, Object>> data = jdbcTemplate.queryForList("SELECT * FROM TICKETS");
return data;
}
}
Jquery
$('#download').click(function () {
var query = $('#query').val();
var type = $('#type').val();
console.log('/get' + type);
$.ajax({
type: 'get',
url: '/get' + type,
data: {query: query},
success: function (result) {
console.log(result);
//download the file
},
error: function () {
$('#message').html('Error downloading the file')
}
})
});