javascript - controller can't return result in JSON object in ajax response -
i have call , fetch data rest api in every second. call method time 1 sec. follows.
var myvar = setinterval(function(){ getdata1() }, 1000); following javascript function call controller.
function getdata1(){ var url=context_root+"/login/getdashboarddata1"; $.ajax({ type: "post", url: url, contenttype: "application/json", datatype: "json", data:{}, success: function(data){ alert(data); }, error: function(e){ //alert(e); } }); } this controller code
@requestmapping(value="/getdashboarddata1", method=requestmethod.post) public jsonobject @responsebody getdashboarddata1() throws jsonparseexception, jsonmappingexception, nullpointerexception{ resttemplate resttemplate = new resttemplate(); string url = "http://localhost:8080/r_f22bc0ac1fe48bce/dataservice/lastdata/"; string user = resttemplate.getforobject(url, string.class); system.out.println("user: "+user); jsonobject obj = null; try { obj = new jsonobject(user); } catch (exception e) { e.printstacktrace(); } return obj; } if run program jsp not shows alert. if change return type of in controller string shows proper json string in ajax response.
i.e. [{"sno":"3618","data":"01","datetime":"2017-04-05 12:33:26.266"}]
if carry this, unable data json string.
please tell me issue. or there other way this.?
you have json array
[{"sno":"3618","data":"01","datetime":"2017-04-05 12:33:26.266"}] then access using index:
data[0].sno
Comments
Post a Comment