1.实体bean:Product.java
public class Product{
private String id;//商品ID
public String getId() { return this.id; } public void setId(String id) { this.id = id; }
2.controller
@RequestMapping(value="/getJson")
@ResponseBody
public void getJson(Product product,@RequestParam String callback,HttpServletRequest request,HttpServletResponse response){
OutputStream out = null;
try {
out = response.getOutputStream();
String str =callback +"('"+JsonUtil.oToJ(product)+"')";
out.write(str.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.前端js调用
如果需要跨域测试,可修改可修改C:\Windows\System32\drivers\etc\hosts
添加
127.0.0.1 ycwebgis.com
$.getJSON('http://ycwebgis.com:8083/smh/getJson?callback=?',
{id:'1'},function(data,status,xhr){
alert(data);
});