首先我是用myecilpse来和hubilder进行跨域相连的,
1、需要在myeclipse里面搭建一个自己的过滤器,代码如下:
//过滤器
public class crossDomainFilter implements Filter{
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) resp;
//这里最好不要写通配符,如果允许多个域请求数据的话,可以直接用逗号隔开:"http://www.baidu.com,http://google.com"
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept,X-Requested-With");
chain.doFilter(req, resp);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
2、然后在web.xml里面吧这个过滤器加上去
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.base.filter.crossDomainFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后就可以根据自己的ip地址,在hbuilder上面写ajax,带上你的代码,你想要上面的代码能运行需要先把myeclipse运行起来,并且只能在同一个局域网上运行
3、我在hbulder上面的ajax代码:
mui.ajax('http://10.21.0.5:8080/storm/material/doSearchapp/',{
data:{
shicai:value
},
dataType:'json',
type:'post',
<!--//crossDomain: true, //强制使用5+跨域-->
timeout:10000,
crossDomain:true,
success:function(data){
var rowData =data.material;
for(var i=0;i<rowData.length;i++){
$("#append").append("<tr><td>"+rowData[i].NAME+"</td></tr>");
}
},
error:function(xhr,type,errorThrown){
alert("88888");
}
});