如何解决“预检无效(重定向)"或“预检请求不允许重定向"

编程入门 行业动态 更新时间:2024-10-27 06:28:08
本文介绍了如何解决“预检无效(重定向)"或“预检请求不允许重定向"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已按照此步骤设置服务器以启用CORS. https ://docs.microsoft/zh-CN/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

I have followed this step to setup my server to enable CORS. docs.microsoft/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

但是现在在我的浏览器开发控制台中,我看到以下错误消息:

But now in my browser dev console, I see this error message:

XMLHttpRequest无法加载 serveraddress/abc .回应 飞行前无效(重定向)

XMLHttpRequest cannot load serveraddress/abc. Response for preflight is invalid (redirect)

您知道该如何解决吗?我正在HTTPS中发出CORS请求.我认为这是导致印前检查无效(重定向)"失败的原因.但是我不知道为什么或什么重定向了OPTIONS请求.

Do you know what can I do to fix it? I am making a CORS request in HTTPS. I think that is causing the 'preflight is invalid (redirect)' failure. But I don't know why or what is redirecting the OPTIONS request.

谢谢.

推荐答案

您的代码正在触发您的浏览器发送 CORS预检OPTIONS请求,并且服务器使用3xx重定向进行响应.它需要用2xx成功消息来响应.

Your code is triggering your browser to send a CORS preflight OPTIONS request, and the server’s responding with a 3xx redirect. It needs to respond with a 2xx success message instead.

您也许可以调整代码以避免触发浏览器发送OPTIONS请求.

You may be able to adjust your code to avoid triggering the browser to send the OPTIONS request.

就这种情况下的所有情况而言,重要的是要知道浏览器在以下情况下会进行CORS的预检:

As far as what all’s going on in this case, it’s important to know browsers do a CORS preflight if:

  • 请求方法不是GET,HEAD或POST
  • 之外的任何其他方法
  • 您已设置了Accept,Accept-Language,Content-Language,Content-Type,DPR,Downlink,Save-Data,Viewport-Width或Width以外的自定义请求标头
  • Content-Type请求标头的值不是application/x-www-form-urlencoded,multipart/form-data或text/plain
  • the request method is anything other than GET, HEAD, or POST
  • you’ve set custom request headers other than Accept, Accept-Language, Content-Language, Content-Type, DPR, Downlink, Save-Data, Viewport-Width, or Width
  • the Content-Type request header has a value other than application/x-www-form-urlencoded, multipart/form-data, or text/plain

如果您无法更改代码以避免浏览器进行预检,那么另一种选择是:

If you can’t change your code to avoid need for browsers to do a preflight, then another option is:

  • 在对OPTIONS请求的响应中,检查Location响应标头中的URL.
  • 更改您的代码以直接向其他网址发出请求.
  • Examine the URL in the Location response header in the response to the OPTIONS request.
  • Change your code to make the request to that other URL directly instead.
  • URL之间的区别可能只是路径中的斜杠一样简单,例如,您可能需要将代码中的URL更改为localhost/api/auth/login/(请注意斜杠)而不是localhost/api/auth/login(没有结尾的斜杠.

    The difference between the URLs might be something as simple as a trailing slash in the path — for example, you may need to change the URL in your code to localhost/api/auth/login/ (notice the trailing slash) rather than localhost/api/auth/login (no trailing slash).

    您可以使用浏览器devtools中的网络"窗格检查对OPTIONS请求的响应,并在Location响应标头的值中找到重定向URL.

    You can use the Network pane in browser devtools to examine the response to the OPTIONS request and to find the redirect URL in the value of the Location response header.

    更多推荐

    如何解决“预检无效(重定向)"或“预检请求不允许重定向"

    本文发布于:2023-10-09 16:43:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1476233.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重定向   不允许   如何解决   quot

    发布评论

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

    >www.elefans.com

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