1.问题性质
这是一个跨域问题
2.解决办法
1.服务端修改
在请求里增加权限如下,本人用的filter方式,
define
/**
* Created by cqs on 16-6-17.
*/
public class CORSFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin","*");
filterChain.doFilter(request,response);
}
}
config
<!--filter-->
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.cqs.bishe.filter.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.请求端修改
ajax举例 dataType jsonp,不了解的可以百度下更多使用方法,个人只提供思路
$.ajax({
type : "get",
async:false,
url : "ajax.ashx",
dataType : "jsonp",
jsonp: "callbackparam",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonpCallback:"success_jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
success : function(json){
alert(json);
alert(json[0].name);
},
error:function(){
alert('fail');
}
});