1.jsonp请求get
controller:
1.继承JsonpAdvice类;
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
2.jsonpAdvice继承org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
3.配置文件
<!--解析返回JSON -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
4.ajax请求实例
function testJsonp(){
$.ajax({
type:"get",
async:false,
url:"http://localhost:8080/demo-app/carKindController/selectByCarGroupId",
data:{zbid:'402880ef0ca9c2b6010cc387c49c0049'
},
dataType:"jsonp",//数据类型为jsonp
jsonp:"callback",//服务端用于接收callback调用的function名的参数
success:function(data){
alert(data.result);
$("#showcontent").text("Result:"+data.result)
},
error:function(){
alert('fail');
}
});
}
2.跨域post请求(解决请求参数过长)
1.java设置返回表头:
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Methods","POST");
response.setHeader("Access-Control-Max-Age","1000");
2.js请求:
function testJsonpLong(){
$.ajax({
type:"post",
crossDomain: true,
url:"http://192.168.120.162:8080/demo-app/partPointController/selectBill",
data:{partList:"{'listPart':[{'diivsionId':null,'partName':'中网框','partCode':'002400','item':'更换','partPrice':'1144','changePrice':'0','repairPrice':'0','paintPrice':'0','gsPrice':'0'},{'diivsionId':null,'partName':'低音喇叭','partCode':'036120','partPrice':'78','partName':'前叶子板内衬(右)','partCode':'026410','partPrice':'763','partName':'前减震器防尘套(右)','partCode':'057551','partPrice':'0','partName':'散热器上护罩','partCode':'040300','partName':'发动机罩内衬','partCode':'007100','partPrice':'1035','partName':'发动机下护板(后)','partCode':'008904','partPrice':'899','partName':'前制动器挡泥板(右)','partCode':'048122','partPrice':'168','partName':'前保险杠电眼底座','partCode':'001526','partPrice':'30','partName':'转向器防尘罩(右)','partCode':'046411','partPrice':'80','partName':'前减震器轴承(右)','partCode':'057530','partPrice':'214','partName':'中网','partCode':'002000','partPrice':'3832','partName':'前轮边制动软管(右)','partCode':'048332','partPrice':'115','partName':'前大灯喷水嘴(右)','partCode':'005210','partPrice':'66','partName':'前保险杠支架(右)','partCode':'001622','partPrice':'254','partName':'发动机罩亮条','partCode':'007210','partPrice':'611','partName':'前保险杠电眼(外)','partCode':'001523','partPrice':'869','partName':'前叶子板内衬后护板(右)','partCode':'026530','partPrice':'216','partName':'前减震器(右)','partCode':'057500','partName':'发动机罩锁(上)','partCode':'007310','partName':'轮胎','partCode':'091020','partName':'变速器油底壳','partCode':'047430','partPrice':'814','partName':'前门玻璃升降电机(右)','partCode':'019310','partPrice':'908','partName':'排气管(前段)','partCode':'107100','partName':'发动机罩','partCode':'007000','item':'维修','partPrice':'3559','partName':'底大边(右)','partCode':'066440','partPrice':'1567','partName':'钢圈','partCode':'091007','partPrice':'1931','partName':'前雾灯护罩(右)','partCode':'005520','partPrice':'223','partName':'油底壳(下)','partCode':'008502','partPrice':'445','partName':'前下摆臂(右)','partCode':'057400','partPrice':'1788','partName':'中网饰条','partCode':'002300','partName':'前大灯(右)','partCode':'005000','partPrice':'6329','partName':'前保险杠皮','partCode':'001100','partPrice':'1601','partName':'前平衡杆胶套','partCode':'054031','partName':'前平衡杆','partCode':'054030','partPrice':'571','partName':'前减震器螺旋弹簧(右)','partCode':'057540','partName':'前保险杠电眼(内)','partCode':'001522','partPrice':'495','partName':'前保险杠电眼线束','partCode':'001521','partPrice':'323','partName':'前减震器缓冲块(右)','partCode':'057550','partPrice':'61','partName':'前平衡杆小连杆(右)','partCode':'054034','partPrice':'517','partName':'前保险杠内衬','partCode':'001600','partPrice':'367','partName':'底盘中间护板(右)','partCode':'089102','partPrice':'933','partName':'前保险杠格栅','partCode':'001427','partPrice':'183','partName':'高音喇叭','partCode':'036110','partName':'发动机罩胶条(前)','partCode':'007220','partPrice':'120','partName':'前门限位器(右)','partCode':'021200','partPrice':'429','partName':'前下摆臂球头(右)','partCode':'057410','partPrice':'316','partName':'排气管(后段)','partCode':'107102','partPrice':'3549','partName':'转向横拉杆(右)','partCode':'046314','partName':'前保险杠牌照板','partCode':'001210','partName':'发动机罩撑杆(右)','partCode':'007402','partPrice':'197','partName':'发动机罩减震胶','partCode':'007510','partName':'中网徽标','partCode':'002200','partPrice':'6763','partName':'前保险杠拖车钩盖板','partCode':'001220','partPrice':'76','partName':'前门玻璃升降机(右)','partCode':'019300','partPrice':'253','partName':'前大灯喷水嘴盖(右)','partCode':'005211','partPrice':'75','partName':'前叶子板(右)','partCode':'026000','partPrice':'1341','partName':'前大灯罩(右)','partCode':'005003','partPrice':'7008','partName':'前门线束(右)','partCode':'036432','partPrice':'510','partName':'前保险杠电眼底座(右)','partCode':'001524','partPrice':'69','partName':'前门玻璃绒槽(右)','partCode':'019130','partPrice':'329','partName':'前上摆臂(右)','partCode':'057300','partPrice':'1806','partName':'转向横拉杆拉杆球头(右)','partCode':'046312','partPrice':'278','partName':'发动机下护板(前)','partCode':'008903','partPrice':'1548','partName':'底大边饰条(右)','partCode':'066442','partName':'前门内防水膜(右)','partCode':'022010','partPrice':'83','partName':'前制动盘(右)','partCode':'048102','partPrice':'1814','partName':'前雾灯(右)','partCode':'005500','gsPrice':'0'}]}"
},
dataType:"json",
success:function(data){
alert('success');
alert(data.result);
},
error:function(){
alert('fail');
}
});
}