我从服务器请求json或xml数据。
我控制台日志,对于JSON,我看到json数据,对于XML,我看到#Document
如何将这些数据分别下载到json文件和xml文件中?
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')
}
})
});