Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have encountered a strange issue with a GET request that I am stuck on.

I am calling a GET request from my ASP.Net application that works fine in postman but does not hit my userGETReq.onload.

function getUser(username){
userGETReq.open("GET", userURL + "/" + username);
userGETReq.send();

userGETReq.onload = () => {if(userGETReq.status === 200){//cool stuff }}

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">

POSTMAN

Picture of successful postman response for the GET request

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)

This is how my CORS is set up

that is the problem

CORS:

        EnableCorsAttribute cors = new EnableCorsAttribute("*","*","*");
        config.EnableCors(cors);

Any help would be greatly appreciated!

The waring I am getting:

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).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
408 views
Welcome To Ask or Share your Answers For Others

1 Answer

to resolve CORS issue, you can write another method in service as follows

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...