首页 \ 问答 \ AngularJS中的同步$资源(Synchronous $resource in AngularJS)

AngularJS中的同步$资源(Synchronous $resource in AngularJS)

我处于死胡同,因为我知道Javascript不是用于同步工作,特别是AngularJS。 但是,我遇到了困难的情况。

我在域名“www”(使用AngularJS)上有一个主页,它调用了很多“api”。 “api”上的一些资源需要身份验证,因此它返回401,然后AngularJS会在它看到时选择并显示一个登录框。

www-login支持一堆登录方法,并将密码/ oauth / whatever发送到“api”,后者返回角度应用程序存储在cookie中的令牌,并设置为$http.defaults.headers.common['Authorization']因此它可用于验证对api的进一步请求。

同样的方法也用于获取属于api-token的用户名。

这完美地工作并解决了我的设计目标。 但是如果浏览器要访问需要身份验证的URL,则会弹出401(因此弹出登录框)。 这是(我猜)因为在401访问浏览器之前,angular无法填充标题中的Authorization字段。

解决方案?

  1. 使用jquery有一个async = false请求?
  2. 完成获取所需数据后,关闭登录框。 登录框可能会闪烁......
  3. 在cookie中存储更多关于登录(即用户名)的元数据,因此我们无需在加载应用程序时从服务器获取此信息。
  4. ??

有更好的解决方案吗? 这一次,对于这一个请求,我想在我的Angular资源中使用async = false ....


I'm in a dead end, because I know that Javascript isn't made for synchronous work, and specially not AngularJS. However, I am in a situation where I am stuck needing it.

I have one main page on the domain "www" (with AngularJS), that calls "api" a lot. Some of the resources on "api" requires authentication, so it returns an 401, which in turn AngularJS picks up and displays a login-box when it sees.

The www-login supports a bunch of login methods and sends the password/oauth/whatever to the "api", which returns a token which the angular app stores in a cookie, and sets in $http.defaults.headers.common['Authorization'] so it can be used to authenticate furter requests to the api.

The same method is also used to get the username that belongs to the api-token.

This works perfectly and solves my design goals. But if the browser is going to an url that requires authentication, the 401 (and hence the login box pops-up). This is (I guess) because angular is not able to populate the Authorization field in the header before the 401 hits the browser..

Solutions?

  1. Have an async=false request using jquery?
  2. Close the login-box when we are done getting the data we want. The login box might flicker...
  3. Store more meta-data about the login (ie, username) in cookies, so we dont haveto do get this information from the server when the app is loading.
  4. ??

Is there a better solution? This one time, for this one request, I want async=false in my Angular resource....


原文:https://stackoverflow.com/questions/18452176
更新时间:2022-02-24 13:02

最满意答案

最终,旧的内存将被释放,但你无法预测何时会发生这种情况。 它取决于Python实现和许多其他因素。

也就是说,对于您给出的示例和CPython实现,旧的数组应该在赋值期间进行垃圾回收。

(请注意,NumPy数组是讨论垃圾收集器行为的一个特别复杂的示例。)


Eventually, the old memory will be freed, though you cannot predict when this will happen. It is dependent on the Python implementation and many other factors.

That said, for the example you gave and the CPython implementation, the old array should be garbage collected during the assignment.

(Note that NumPy arrays are a particularly complex example for discussing garbage-collector behaviour.)

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。