[
{"id":1,"name":"Michael","score":8.5},
{"id":2,"name":"Naomi","score":5.6}
]
angular.module("app", []).controller("listController", function($scope, $http)
{
var url = 'https://abc.cfapps.io/students';
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', url, true);
httpRequest.setRequestHeader('Access-Control-Allow-Origin', '*');
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.onerror = function (XMLHttpRequest, textStatus, errorThrown) {
console.log('failed');
console.log(JSON.stringify(XMLHttpRequest));
};
httpRequest.onload = function () {
console.log('SUCCESS!');
}
httpRequest.send();
});
@RestController
@CrossOrigin(origins = "http://localhost:52442")
@RequestMapping(value="/students")
public class StudentService
{
@RequestMapping(value="/",method = RequestMethod.GET)
public ArrayList<Student> getListStudents()
{
// return list
}
// other methods
}
但我不断得到这样的错误:
XMLHttpRequest cannot load https://abc.cfapps.io/students.
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:52442' is therefore not allowed access. The response had HTTP status code 403.
如果在后端使用java代码
我们可以通过为它创建一个类来尝试以这种方式配置它
package com.web;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Note this is a very simple CORS filter that is wide open.
* This would need to be locked down.
*/
@Component
public class CORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
我想这可能有用
我有一个网络服务,比如: 在链接中: 我正在尝试通过链接中的AlFresco获取此Web服务的响应:。我现在有不同的端口(当我有相同的端口时,这很好地工作),所以,使用不同的端口,我在AlFresco中得到错误: 跨源请求被阻止:同一源策略不允许在以下位置读取远程资源:http://localhost:8081/ChangesPDF/Changes?...(原因:缺少CORS的“访问控制允许原点”
虽然已经通过API网关设置了CORS,并且设置了头,但在尝试在Chrome内从AJAX调用API时,我仍然收到以下错误: XMLHttpRequest无法加载http://xxxxx.execute-api.us-west-2.amazonaws.com/beta/yyyyy。请求的资源上没有“access-control-allow-origin”标头。因此不允许访问源“null”。响应的HTT
Access-Control-Allow-Origin响应 header 指示是否该响应可以与具有给定资源共享原点。 Header type Response header Forbidden header name no 语法 Access-Control-Allow-Origin: *Access-Control-Allow-Origin: <origin> 指令 * 对于没有凭据的请求,服务
我知道这件事以前被处理过很多次。但是我可以在响应中看到头上已经有了Access-Control-Allow-Origin。 accept:/ accept-encoding:gzip,deflate,sdch,br access-control-request-headers:x-an-webservice-identitykey access-control-request-method:pos
问题内容: 我使用球衣JAX-Rs作为Web服务来查询mysql。我尝试通过混合移动应用程序使用Web服务。 我指的是这个 http://coenraets.org/blog/2011/11/building-restful-services-with-java- using-jax-rs-and-jersey-sample- application/#comment-440541 在服务器端,以
问题内容: 将web.xml移植到Java配置后出现以下问题 根据一些Spring参考,尝试了以下尝试: 所选择的值来自有效的web.xml过滤器: 有什么想法为什么Spring java config方法不能像web.xml文件那样工作? 问题答案: 将CorsMapping从更改方法。 为整个应用程序启用CORS很简单: 你可以轻松更改任何属性,以及仅将此CORS配置应用于特定的路径模式: 控