ajax跨域
index.html的ajax请求get方式请求
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<metacharset="utf-8">
<metahttp-equiv="Content-Type"content="text/javascript;charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<scripttype="text/javascript"src="jquery.min.js"></script>
<script>
//alert(app);
$(function(){
$.ajax({
url:'http://xxxxxxxxx?url=http://item.jd.com/1057746.html&func=jpc',
type:"get",
//cache:false,
dataType:"jsonp",
jsonp:"func",
jsonpCallback:"jpc",
crossDomain:true,
timeout:20000,
success:function(data){
varobj=eval(data)
alert(obj.length);
},
error:function(data){
alert("dddddddd");
}
});
});
</script>
</body>
</html>
后台调用:(springmvc返回的callback的json)
/**
*@authorliyu
*
*/
@Controller
publicclassPluginController{
privateLoggerlog=Logger.getLogger(PluginController.class.getName());
privatestaticRandomrandom=newRandom();
@Autowired
privatePlugin_GuessServiceplugin_GuessService1;
@Autowired
privateAllMallDaoallMallDao;
@RequestMapping(value="/guess/yiqifa",method=RequestMethod.GET)
@ResponseBody
publicStringQueryPlugin(@RequestParamStringurl,HttpServletResponseresponse,HttpServletRequestrequest)throwsIOException{
response.setCharacterEncoding("utf8");
//response.setContentType("text/html;charset=utf-8");
//response.setHeader("Access-Control-Allow-Origin","*");
Stringfunc=request.getParameter("func");
if(null==url||"".equals(url)){
returnnull;
}
Stringcat1=null;
Stringcat2=null;
StringurlTemp=url.substring(url.indexOf(".")+1);
inturlIndexOf=urlTemp.indexOf("/");
StringdomainName=urlTemp.substring(0,urlIndexOf==-1?urlTemp.length():urlIndexOf);
StringitemId=getItemId(allMallDao.selectByDomainName(domainName),url);
List<Map<String,Object>>listItem=SolrStatement.query("id:"+itemId);
if(listItem!=null&&!listItem.isEmpty()){
Map<String,Object>mapItem=listItem.get(0);
if(mapItem==null||mapItem.isEmpty()){
log.info("nofoundcat1andcat2,pleasecheckyoururl");
}
if(mapItem.size()>0){
cat1=(String)mapItem.get("cat1");
cat2=(String)mapItem.get("cat2");
}
//price=(String)mapItem.get("price");
}else{
log.info("nofoundcat1andcat2,pleasecheckyoururl");
}
Plugin_Guesspg=newPlugin_Guess();
pg.setLv1(cat1);
pg.setLv2(cat2);
List<Plugin_Guess>listPG=plugin_GuessService1.selectByCat1(pg);
StringBuffersb=newStringBuffer();
sb.append("[");
if(listPG.size()>0&&listPG.size()<=15){
for(inti=0;i<listPG.size();i++){
Plugin_Guesso=listPG.get(i);
//转化为json
sb.append("{\"url\":\""+o.getSp_url()+"\",\"url_image\":\""+o.getImage_url()+"\",\"price\":\""+o.getPrice()+"\",\"title\":\""+o.getTitle()+"\"},");
}
}elseif(listPG.size()>15){
Set<Integer>set=rankRandom(listPG.size());
List<Set<Integer>>listValue=Arrays.asList(set);
Set<Integer>setV=listValue.get(0);
Iterator<Integer>itr=setV.iterator();
while(itr.hasNext()){
intk=itr.next();
Plugin_Guesso=listPG.get(k);
//转化为json
sb.append("{\"url\":\""+o.getSp_url()+"\",\"url_image\":\""+o.getImage_url()+"\",\"price\":\""+o.getPrice()+"\",\"title\":\""+o.getTitle()+"\"},");
}
}
Stringstr="";
if(listPG.size()>0){
str=sb.substring(0,sb.lastIndexOf(","));
}else
str=sb.toString();
str=str+"]";
log.info(str);
if(null!=func&&!"".equals(func)&&"jpc".equals(func)){
str=func+"("+str+")";
}
returnstr;
}
/**
*@paramallMall
*@paramurl
*@return
*/
publicstaticStringgetItemId(All_MallallMall,Stringurl){
StringregexItemId=allMall.getRegex_itemid();
Matchermatcher=Pattern.compile(regexItemId).matcher(url);
if(matcher.find()){
returnallMall.getItem_id_prefix()+"_"+matcher.group();
}else{
returnnull;
}
}
/**构造一个大小为15的集合并且数据中没有重复的
*@params
*@return
*/
publicstaticSet<Integer>rankRandom(intlistSize){
Set<Integer>set=newHashSet<Integer>();
while(set.size()<15){
ints=random.nextInt(listSize);
set.add(s);
}
returnset;
}
}
ajax跨域post提交:(采用cors)
http://www.cnblogs.com/Darren_code/p/cors.html
相关推荐
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo