- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
请耐心等待,这需要一些解释。
我正在帮助构建一个混合移动网络应用程序。主要代码库是 HTML5 和 JavaScript,它们将被包装在原生移动 Web View (类似于 Phonegap)中。
部分功能要求应用程序将信息发布到由我们的一位客户控制的网络服务。更改此 Web 服务的范围很小,因为其他人正在使用它。我们使用 HTTP POST 发送 JSON 并从服务器接收响应。此响应的一部分是一个 JSESSIONID cookie,它管理我们与服务器的 session 。初始initSession()
之后调用时,我们需要在每个 (AJAX) 请求中发送 JSESSIONID cookie。
当部署在移动设备上时,网络应用程序被包装在 native WebView 中,通过浏览到 file:///path/to/app/index.html
启动网络应用程序。 .
我们尝试的第一件事是要求我们的客户设置 Access-Control-Allow-Origin: *
在他们的响应 header 中允许 CORS。然后我们尝试发布到服务器:
$.ajax({
url: 'http://thirdparty.com/ws',
data: data,
type: "POST",
dataType: "JSON",
success: successCallback,
error: failedCallback
});
监控请求,很明显没有包含 cookie。仔细观察有一个 special section in the CORS spec for dealing with user credentials ,其中包括 session cookie。所以我修改了 AJAX 调用以包含以下内容:
$.ajax({
url: 'http://thirdparty.com/ws',
data: data,
type: "POST",
dataType: "JSON",
success: successCallback,
error: failedCallback,
xhrFields { withCredentials: true }
});
另一个错误,这次来自浏览器。更多阅读产生了以下内容:
If the third party server did not respond with an
Access-Control-Allow-Credentials: true
header the response would be ignored and not made available to web content.Important note: when responding to a credentialed request, the server must specify a domain in the
Access-Control-Allow-Origin
header, and cannot use wild carding.
所以我们需要更改服务器的 header 以包含Access-Control-Allow-Credentials: true
和 Access-Control-Allow-Origin
到我们的起源。
我们终于到了我的问题:when loading a web page using the file:// protocol , Origin
从 Web View 发送的请求 header 设置为 null
.因此它不能被服务器解析,所以服务器不能在 Access-Control-Allow-Origin
中设置它。 .但是如果服务器不能设置Access-Control-Allow-Origin
到*
以外的东西我们无法发送凭证,包括 cookie。
所以我卡住了。该怎么办? I saw a similar question posted here但我真的不明白建议的答案。任何帮助将不胜感激!
最佳答案
我意识到这个问题很老了,但我想无论如何我都会参与其中。对于 CORS 请求,浏览器会对它们进行预检。这意味着 - 无论您使用何种 $.ajax()
方法,都会向服务器发送一个 OPTIONS
请求。
这个预检 OPTIONS
请求实际上是在说:
"Hey there, foreign-server-from-some-other-domain, I want to send you a not-simple request (simple req's are not preflighted). My not-simple request going to have these kinds of headers and content type and so on. Can you let me know if this is okay?"
然后服务器将执行它所做的任何操作(可能检查某些配置或数据库)并以允许的来源、允许的 header 和/或允许的方法进行响应。
最后 - 如果预检 OPTIONS
请求收到允许实际 $.ajax()
方法运行的响应 - 它运行。
CORS 与 JSONP 不同。
综上所述 - 虽然 withCredentials
预检成功需要响应携带 Access-Control-Allow-Credentials
header (如问题中所述),即 IN添加 Access-Control-Allow-Origins
和 Access-Control-Allow-Methods
值,其中必须包括预期请求的各个方面。
例如 - 如果您从来源 http://foo-domain.com
发出 CORS POST
请求, header somevalue
到http://bar-domain.com
,预检 OPTIONS
请求将发出,以便向 http://发出实际的发布请求bar-domain.com
,OPTIONS
请求需要接收一个包含 http 的
。这可以是原始名称本身或 Access-Control-Allow-Origins
值的响应://foo-domain.com*
。响应还需要有一个包含 POST
的 Access-Control-Allow-Methods
值。这也可能是 *
。最后,如果我们希望我们的 somevalue
header 被允许,则响应必须包含一个 Access-Control-Allow-Headers
值,其中包含我们的 somevalue
header 键或 *
。
回过头来 - 如果您无法控制服务器,或者无法让服务器允许您的 CORS 请求,您可以始终使用 JSONP 或某些 urlEncoded 数据类型和/或发出没有自定义 header 的简单请求。 GET
、HEAD
和完整的 POST
请求通常是简单的请求。
关于javascript - 使用文件 ://在本地加载的来自移动 WebView 的 CORS cookie 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9103876/
我有一个问题,我不断收到错误 没有为“svn.ssl.server”凭据注册的提供者 我正在使用在另一台 SVN 服务器上工作的相同代码,但我设置的新服务器似乎无法连接,即使我可以通过 Web 浏览器
如何通过 shell 在 Hudson 中输入 subversion 凭据? 我尝试在 HUDSON_HOME 中生成文件 hudson.scm.SubversionSCM.xml 并重新加载配置,但
我正在尝试使用 powershell 访问远程注册表,如下所示: $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachi
我需要将凭据存储在 powershell 中以便多次使用。 StackOverflow 上有很多例子,所以我拿了一个 $tmpCred = Get-Credential $tmpCred.Passwo
我遇到了 youtube java 凭据的问题,通常它运行良好并且我能够上传到 youtube,但今天我收到此异常无效的凭据。 YouTubeService service = new YouTube
我正在阅读中提供的 Hadoop 凭证文档 https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/Crede
我想知道在为 MySQL 凭据/主机创建变量时最佳做法或建议做什么。 define('HOST', 'localhost'); // etc.. mysql_connect(HO
我试图让 Jenkins 从 BitBucket 克隆我的 mercurial 项目。它不会,因为它说凭据有问题 - 好吧,bitbucket 拒绝 Jenkins 提供的任何内容。 我几乎 100%
这里有一百万篇关于如何使用 git 缓存凭据的帖子。但是,如果机器重新启动,它们似乎都不成立。有没有办法缓存在机器重新启动时持续的凭据? 最佳答案 是的,在 Debian 和 Ubuntu 上,您可以
我正在尝试在我的环境中为 IIS 节点使用共享配置,我想使用组托管服务帐户凭据来实现这一目标。 当我将应用程序池的凭据应用为 MyDomain\GmsaAccount$ 时,它运行良好,但是当我尝试在
我创建了一个应用程序,它充当 2 个不同 API(WebEx 和 Exchange Web 服务)和电子邮件之间的桥梁。用户向一个特殊的电子邮件地址发送日历邀请,该应用程序解析 ICS 并创建一个 W
我正在尝试将凭据从 Jenkins 迁移到另一个凭据存储。 我想从 Jenkins 商店读取凭据,并找到了这个脚本 ( https://github.com/tkrzeminski/jenkins-g
我有一个在 Windows 上运行的 Jenkins 服务器。它将用户名:密码存储在凭据插件中。这是一个定期更新密码的服务用户。 我正在寻找一种运行脚本的方法,最好是 Powershell,它将更新
我想知道如何创建 Jenkins 和 Jenkins 中运行的作业可以使用的凭据以连接到 3rd 方服务。 最佳答案 您应该指定您将使用的第 3 方服务。 以下是带有 的凭据示例bitbucket 我
我正在一个网站上工作,我们希望使用 Spring Security Kerberos 使用 Kerberos 身份验证。所以,我们不支持 NTLM。当用户发出未经身份验证的请求时,服务器将回复带有 h
如果我设置 git config --global credential.username my_username 选项,然后使用 --local 选项覆盖一个存储库,这并没有什么区别 - 它在尝试提
我正在尝试使用需要 gce_client_id 和 gce_client_secret key 的第 3 方应用程序。为了生成它们,我浏览了凭据图标并尝试创建一个 OAuth 2.0 客户端 ID。但
我在使用 Azure 时遇到身份验证问题。我有一个运行 powershell 脚本的连续构建服务器,我收到如下消息: Your Azure credentials have not been set
首先,我想说我在安全和身份验证方面的知识非常有限。 我有一个应用程序可以从 docker store 中提取和运行容器。这是一个私有(private)仓库,所以我需要传递用户名和密码,以便用户可以拉取
我使用 Google 文档中的代码(如下所示)来管理 Google 日历。 public class CalendarQuickstart { private static final Str
我是一名优秀的程序员,十分优秀!