var myUrl = "http://10.1.20.9:8080/bingo-web/system/listWapOutJson.vhtml?wuLiaoID=40010000040";
$http.get(myUrl).success(function (response) {
if(response.success){
console.log("2123");
$scope.chat = response.data;
}else {
console.log("qweqwe");
}
})
这样写就会出现以下错误,
XMLHttpRequest cannot load http://10.1.20.9:8080/bingo-web/system/listWapOutJson.vhtml?wuLiaoID=40010000040. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.40.6:8100' is therefore not allowed access.
求大神,这个问题应该怎么解决呀?
很明显,跨域了嘛。两种解决方法
1:看看ng有没有自己的前台的解决方法,比如ajax的jsonp
2:后台配置允许这个方法跨域
楼上说的对 跨域问题 我感觉如果是平时自己学习,对安全,速度要求不高的话可以试试这个第三方api https://bird.ioliu.cn/
后台接口配置下允许跨域请求就可以
后台在缺省时,是不许可的跨域请求的。百度 cors。以下是spring boot的代码。
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class SecurityCorsConfiguration {
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://localhost:4200");
config.addAllowedHeader(CorsConfiguration.ALL);
config.addAllowedMethod(CorsConfiguration.ALL);
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}