function getJson(){
$.ajax({
type:"get",
dataType:"json",
url:"<%=basePath %>getJson",
success:function(data){
for(var i=0;i<jsonData.length;i++){
alert("Id:"+data[i].id+" username:"+data[i].username);
}
},
error:function(e){
alert(e);
}
})
}
@ResponseBody
@RequestMapping("/getJson")
public Object getJson(){
return new Object();
}
注意:
使用手动写的json工具类
@RequestMapping("/getJson")
public void getJson(HttpServletResponse response){
Object obj = new Object();
JSONObject json = JSONObject.fromObject(obj);
JsonUtils.ajaxJson(json.toString(), response);
}
附上JsonUtils工具类的代码:
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
public class JsonUtils {
public static void ajaxJson(String jsonString,HttpServletResponse response) {
ajax(jsonString, "application/json",response);
}
public static void ajax(String content, String type,HttpServletResponse response) {
try {
response.setContentType(type + ";charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(content);
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有