GET请求适用于邮递员,但不适用于浏览器

编程入门 行业动态 更新时间:2024-10-26 22:23:21
本文介绍了GET请求适用于邮递员,但不适用于浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一个我遇到的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请求的邮递员响应图片

我从同一应用程序收到其他有效的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 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 ("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, "localhost:4200") .header(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS, "content-type") .build(); } else { return Response.ok().build(); } }

更多推荐

GET请求适用于邮递员,但不适用于浏览器

本文发布于:2023-10-29 13:19:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539799.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   邮递员   但不   浏览器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!