• <tfoot id='WpJyo'></tfoot>

      • <bdo id='WpJyo'></bdo><ul id='WpJyo'></ul>

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

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

        <legend id='WpJyo'><style id='WpJyo'><dir id='WpJyo'><q id='WpJyo'></q></dir></style></legend>
      1. 使用 Polymer Iron-ajax 和 Node.js 发出 CORS 请求

        Make CORS Request with Polymer iron-ajax and Node.js(使用 Polymer Iron-ajax 和 Node.js 发出 CORS 请求)

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

            <tbody id='GTams'></tbody>
        1. <small id='GTams'></small><noframes id='GTams'>

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

                <legend id='GTams'><style id='GTams'><dir id='GTams'><q id='GTams'></q></dir></style></legend>
                <tfoot id='GTams'></tfoot>
                  本文介绍了使用 Polymer Iron-ajax 和 Node.js 发出 CORS 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试使用 Polymer 和 Node 检索数据,但正在努力获得有效的响应.我收到一个飞行前响应错误,提示 access-control-allow-origin 是不允许的.

                  I am trying to retrieve data using Polymer and Node, but am struggling to get a valid response back. I get a pre-flight response error that says the access-control-allow-origin is not allowed.

                  我在 localhost:4001 上运行 Polymer,在 localhost:8080 上运行 Node.

                  I am running Polymer on localhost:4001 and Node on localhost:8080.

                  如何配置节点或客户端以加载响应?

                  How can I configure either Node or the Client to load a response?

                  客户

                  <iron-ajax id="ajaxUser"
                    url="http://localhost:8080/node/api/mssql/login"
                    method="post"
                    handle-as="json"
                    Content-Type="application/json"
                    headers='{"Access-Control-Allow-Origin": "*"}'
                    params="[[params]]"
                    on-response="saveUserCredentials"
                    last-response="{{user}}"></iron-ajax>
                  

                  节点

                  const corsOptions = {
                    allowedHeaders: ['Content-Type', 'Access-Control-Allow-Origin']
                  }
                  
                  app.options('*', cors(corsOptions))
                  

                  ...

                  app.use((req, res, next) => { // Enable Cross-Origin Resource Sharing (CORS)
                    res.header("Access-Control-Allow-Origin", "*")
                    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
                    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-api-key")
                    next()
                  })
                  

                  错误

                  加载失败
                  http://localhost:8080/login?username=user&password=password:
                  请求的资源上不存在Access-Control-Allow-Origin"标头.
                  因此,Origin 'http://localhost:4001' 是不允许访问的.响应的 HTTP 状态代码为 400.

                  Failed to load
                  http://localhost:8080/login?username=user&password=password:
                  No 'Access-Control-Allow-Origin' header is present on the requested resource.
                  Origin 'http://localhost:4001' is therefore not allowed access. The response had HTTP status code 400.

                  推荐答案

                  问题代码段中的Node配置没有处理OPTIONS请求.

                  The Node configuration in the code snippet in the question doesn’t handle OPTIONS requests.

                  为了确保正确处理 CORS 预检,考虑安装 npm cors 包:

                  To ensure CORS preflights get handled correctly, consider installing the npm cors package:

                  npm install cors
                  

                  然后做这样的事情:

                  var express = require('express')
                    , cors = require('cors')
                    , app = express();
                  app.options('*', cors()); // preflight OPTIONS; put before other routes
                  

                  这将处理预检响应和其他 CORS 方面,而无需您在应用程序代码中从头开始手动编写自己的处理.

                  That’ll handle the preflight response and other CORS aspects without you needing to manually write your own handling from scratch in your application code.

                  https://www.npmjs.com/package/cors#configuration-option 提供了所有选项的更多详细信息.

                  https://www.npmjs.com/package/cors#configuration-option has more details on all the options.

                  这篇关于使用 Polymer Iron-ajax 和 Node.js 发出 CORS 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
                  How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)
                  WebKit quot;Refused to set unsafe header #39;content-length#39;quot;(WebKit “拒绝设置不安全的标头‘内容长度’)
                  $.ajax call working fine in IE8 and Doesn#39;t work in firefox and chrome browsers($.ajax 调用在 IE8 中运行良好,但在 Firefox 和 chrome 浏览器中不起作用)
                  How do I know if jQuery has an Ajax request pending?(我如何知道 jQuery 是否有待处理的 Ajax 请求?)
                  • <bdo id='DGBpF'></bdo><ul id='DGBpF'></ul>
                        <tbody id='DGBpF'></tbody>
                    • <tfoot id='DGBpF'></tfoot>
                      <legend id='DGBpF'><style id='DGBpF'><dir id='DGBpF'><q id='DGBpF'></q></dir></style></legend>

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

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