springmvc+ajax_jsonp跨域问题

来源:互联网 发布:天际捏脸数据 编辑:程序博客网 时间:2024/05/16 18:04

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")@ResponseBodypublic 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);}); 


0 0