gpt4 book ai didi

javascript - 将重定向请求中的 header 抓取到 angular-js 应用程序

转载 作者:行者123 更新时间:2023-12-01 05:01:03 24 4
gpt4 key购买 nike

我有以下情况:我有两个应用程序需要相互通信:一个 JSF 登录页面和一个 angular-js 应用程序。我无法修改 JSF 登录页面。我可以修改 Angular 应用程序。

用户通过访问 JSF 登录页面来登录。用户从浏览器访问 JSF 页面并填写登录表单。如果成功通过身份验证,JSF 将生成一个 token 并将该 token 发送到 header 中,并重定向到具有 angular-js 应用程序的 index.html。 angular-js 应用程序如何从该重定向中获取该 token ?

最佳答案

这取决于您需要什么样的请求头。

如果是 xhr ,很可能你会用 http 做你的请求服务。它的 promise 得到(在两种状态下:失败和成功)object作为带有 header() 的参数方法,您需要的方法。您可以获得更多信息 here :

The response object has these properties:

  • data - {string|Object} – The response body transformed with the transform functions.
  • status - {number} – HTTP status code of the response.
  • headers - {function([headerName])} – Header getter function.
  • config - {Object} – The configuration object that was used to generate the request.
  • statusText {string} – HTTP status text of the response.


编辑

当您需要获取不是来自 xhr 的 header 时会遇到困难但是从初始页面请求开始,在 angular 被引导之前。不幸的是 javascript 无法访问它们。

一种解决方案可能是使用 cookies 而不是标题来存储您需要的数据。但是在这种情况下,您应该在请求发射器端做一些更改。

另一种方式,可能是在提供 html 之前处理服务器上的请求。之后,您可以直接在标记中传递它们,或者以某种方式记住它们,然后发送 xhr直接从 angular 请求它们。但是如果你有一个简单的静态页面,你将无法实现它。

关于javascript - 将重定向请求中的 header 抓取到 angular-js 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32659613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com