js跨域资源共享cros与jsonp
一.js跨域资源共享cros(Cross-origin resource sharing)
该方式是W3C官方提供的js跨域资源共享解决方案
a.后台响应设置头信息
response.setHeader("Access-Control-Allow-Origin", "http://localhost:9105");
response.setHeader("Access-Control-Allow-Credentials", "true");
或者
在方法上加注解
@CrossOrigin(origins="http://localhost:9105",allowCredentials="true") //allowCredentials="true",此项为默认,可以省略
b. 前台请求设置
$http.get('http://localhost:9107/cart/addGoodsToCartList.do?itemId='
+ $scope.sku.id +'&num='+$scope.num,{'withCredentials':true})
二.JSONP
利用<script src=" "></script> 原理,传送数据格式为js文件格式;