<legend id='8MmXe'><style id='8MmXe'><dir id='8MmXe'><q id='8MmXe'></q></dir></style></legend>
  • <tfoot id='8MmXe'></tfoot>
  • <i id='8MmXe'><tr id='8MmXe'><dt id='8MmXe'><q id='8MmXe'><span id='8MmXe'><b id='8MmXe'><form id='8MmXe'><ins id='8MmXe'></ins><ul id='8MmXe'></ul><sub id='8MmXe'></sub></form><legend id='8MmXe'></legend><bdo id='8MmXe'><pre id='8MmXe'><center id='8MmXe'></center></pre></bdo></b><th id='8MmXe'></th></span></q></dt></tr></i><div id='8MmXe'><tfoot id='8MmXe'></tfoot><dl id='8MmXe'><fieldset id='8MmXe'></fieldset></dl></div>

      <small id='8MmXe'></small><noframes id='8MmXe'>

        • <bdo id='8MmXe'></bdo><ul id='8MmXe'></ul>

        hapi.js Cors Pre-flight 不返回 Access-Control-Allow-Origin 标头

        hapi.js Cors Pre-flight not returning Access-Control-Allow-Origin header(hapi.js Cors Pre-flight 不返回 Access-Control-Allow-Origin 标头)

            <tbody id='isXqe'></tbody>

            <legend id='isXqe'><style id='isXqe'><dir id='isXqe'><q id='isXqe'></q></dir></style></legend>

              <i id='isXqe'><tr id='isXqe'><dt id='isXqe'><q id='isXqe'><span id='isXqe'><b id='isXqe'><form id='isXqe'><ins id='isXqe'></ins><ul id='isXqe'></ul><sub id='isXqe'></sub></form><legend id='isXqe'></legend><bdo id='isXqe'><pre id='isXqe'><center id='isXqe'></center></pre></bdo></b><th id='isXqe'></th></span></q></dt></tr></i><div id='isXqe'><tfoot id='isXqe'></tfoot><dl id='isXqe'><fieldset id='isXqe'></fieldset></dl></div>

              <small id='isXqe'></small><noframes id='isXqe'>

                <bdo id='isXqe'></bdo><ul id='isXqe'></ul>

                <tfoot id='isXqe'></tfoot>

                1. 本文介绍了hapi.js Cors Pre-flight 不返回 Access-Control-Allow-Origin 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个使用 (Dropzone js) 上传的 ajax 文件.它将文件发送到我的 hapi 服务器.我意识到浏览器发送了一个 PREFLIGHT OPTIONS METHOD.但我的 hapi 服务器似乎没有发送正确的响应标头,所以我在 chrome 上遇到错误.这是我在 chrome 上遇到的错误

                  I have an ajax file upload using (Dropzone js). which sends a file to my hapi server. I realised the browser sends a PREFLIGHT OPTIONS METHOD. but my hapi server seems not to send the right response headers so i am getting errors on chrome. here is the error i get on chrome

                  XMLHttpRequest cannot load http://localhost:3000/uploadbookimg. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
                  

                  这是 hapi js 路由处理程序

                  this is the hapi js route handler

                  server.route({
                          path: '/uploadbookimg',
                          method: 'POST',
                          config: {
                              cors : true,
                              payload: {
                                  output: 'stream',
                                  parse: true,
                                  allow: 'multipart/form-data'
                              },
                          handler: require('./books/webbookimgupload'),
                          }
                      });
                  

                  据我了解,hapi js 应该从 Pre-fight (OPTIONS) 请求中发送所有 cors 标头.不明白为什么不是

                  In my understanding hapi js should send all cors headers from the Pre-fight (OPTIONS) request. Cant understand why its is not

                  来自 chrome 的网络请求/响应

                  Network request /response from chrome

                  **General**
                  Request Method:OPTIONS
                  Status Code:200 OK
                  Remote Address:127.0.0.1:3000
                  
                  **Response Headers**
                  view parsed
                  HTTP/1.1 200 OK
                  content-type: application/json; charset=utf-8
                  cache-control: no-cache
                  vary: accept-encoding
                  Date: Wed, 27 Apr 2016 07:25:33 GMT
                  Connection: keep-alive
                  Transfer-Encoding: chunked
                  
                  **Request Headers**
                  view parsed
                  OPTIONS /uploadbookimg HTTP/1.1
                  Host: localhost:3000
                  Connection: keep-alive
                  Pragma: no-cache
                  Cache-Control: no-cache
                  Access-Control-Request-Method: POST
                  Origin: http://localhost:4200
                  User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36
                  Access-Control-Request-Headers: accept, cache-control, content-type
                  Accept: */*
                  Referer: http://localhost:4200/books/upload
                  Accept-Encoding: gzip, deflate, sdch
                  Accept-Language: en-US,en;q=0.8
                  

                  提前致谢

                  推荐答案

                  hapi cors: true 是一个通配符规则,允许来自所有域的 CORS 请求,除了少数情况,包括有额外的hapi 的默认白名单之外的请求标头:

                  The hapi cors: true is a wildcard rule that allows CORS requests from all domains except for a few cases including when there are additional request headers outside of hapi's default whitelist:

                  [accept"、authorization"、content-type"、if-none-match"、origin"]

                  请参阅 API 文档中路由选项下的 cors 选项部分:

                  headers - 允许的标头的字符串数组('Access-Control-Allow-Headers').默认为 ['Accept', 'Authorization', 'Content-Type', 'If-None-Match'].

                  headers - a strings array of allowed headers ('Access-Control-Allow-Headers'). Defaults to ['Accept', 'Authorization', 'Content-Type', 'If-None-Match'].

                  additionalHeaders - 附加标头到标头的字符串数组.使用它来保留默认标题.

                  additionalHeaders - a strings array of additional headers to headers. Use this to keep the default headers in place.

                  您的问题是 Dropzone 发送了几个标题以及不在此列表中的文件上传:

                  Your problem is that Dropzone sends a couple of headers along with the file upload that aren't in this list:

                  • x-requested-with(不在您上面的标题中,而是为我发送的)
                  • 缓存控制
                  • x-requested-with (not in your headers above but was sent for me)
                  • cache-control

                  您有两个选项可以让事情正常运行,您需要在服务器或客户端上进行更改:

                  You have two options to get things working, you need to change something on either the server or the client:

                  server.route({
                      config: {
                          cors: {
                              origin: ['*'],
                              additionalHeaders: ['cache-control', 'x-requested-with']
                          }
                      },
                      method: 'POST',
                      path: '/upload',
                      handler: function (request, reply) {
                  
                          ...
                      }
                  });
                  

                  选项 2 - 告诉 dropzone 不要发送那些额外的标头

                  通过他们的配置尚不可能,但有一个待定的 PR 允许它:https://github.com/enyo/dropzone/pull/685

                  这篇关于hapi.js Cors Pre-flight 不返回 Access-Control-Allow-Origin 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                  XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                  quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
                  How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)

                  <small id='NLaD1'></small><noframes id='NLaD1'>

                      <tbody id='NLaD1'></tbody>

                    <tfoot id='NLaD1'></tfoot>
                    • <bdo id='NLaD1'></bdo><ul id='NLaD1'></ul>

                        <i id='NLaD1'><tr id='NLaD1'><dt id='NLaD1'><q id='NLaD1'><span id='NLaD1'><b id='NLaD1'><form id='NLaD1'><ins id='NLaD1'></ins><ul id='NLaD1'></ul><sub id='NLaD1'></sub></form><legend id='NLaD1'></legend><bdo id='NLaD1'><pre id='NLaD1'><center id='NLaD1'></center></pre></bdo></b><th id='NLaD1'></th></span></q></dt></tr></i><div id='NLaD1'><tfoot id='NLaD1'></tfoot><dl id='NLaD1'><fieldset id='NLaD1'></fieldset></dl></div>

                        <legend id='NLaD1'><style id='NLaD1'><dir id='NLaD1'><q id='NLaD1'></q></dir></style></legend>