Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

asp.net - GET request working on postman but not in browser

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...