问题描述
我遇到了一个我遇到的GET请求的奇怪问题。
I have encountered a strange issue with a GET request that I am stuck on.
function getUser(username){
userGETReq.open("GET", userURL + "/" + username);
userGETReq.send();
userGETReq.onload = () => {if(userGETReq.status === 200){//cool stuff }}
我正在跑步在浏览器中的本地主机上-从返回false的表单中调用启动该函数。
I am running on a localhost in the browser - the function to start this is being called from a form that returns false.
<form onsubmit="login(this); return false">
邮递员
我从同一应用程序收到其他有效的GET请求。
此功能与另一个有效功能之间的 only 区别在于,它具有传入的变量并具有设定的路径:
I have other GET requests from the same application that work.The only difference between this and the other one that works is that it has a 'variable' that gets passed in and has a set route:
[Route("api/User/{username}")]
public List<User> Get(string username)
这是设置我的CORS的方式
This is how my CORS is set up
EnableCorsAttribute cors = new EnableCorsAttribute("*","*","*");
config.EnableCors(cors);
任何帮助将不胜感激!
我得到的警告:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:56390/api/user/test3. (Reason: CORS request did not succeed).
推荐答案
要解决CORS问题,您可以在如下所示的服务
to resolve CORS issue, you can write another method in service as follows
每次进行服务调用时,首先触发OPTIONS来检查是否允许该服务调用,并且一旦OPTIONS返回允许,就会调用实际方法
//您可以在HEADER_AC_ALLOW_ORIGIN下添加呼叫主机的URL或客户端URL
Every time service call is made, OPTIONS is triggered first to check if the service call is allowed and once the OPTIONS returns allowed, actual method is invoked//here you can add URL of calling host or the client URL under HEADER_AC_ALLOW_ORIGIN
@OPTIONS
@Path("/yourservice/")
@LocalPreflight
public Response options() {
String origin = headers.getRequestHeader("Origin").get(0);
LOG.info(" In options!!!!!!!!: {}", origin);
if ("http://localhost:4200".equals(origin)) {
return Response.ok()
.header(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, "GET,POST,DELETE,PUT,OPTIONS")
.header(CorsHeaderConstants.HEADER_AC_ALLOW_CREDENTIALS, "false")
.header(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN, "http://localhost:4200")
.header(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS, "content-type")
.build();
} else {
return Response.ok().build();
}
}
这篇关于GET请求适用于邮递员,但不适用于浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!