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

      <tfoot id='wtSfG'></tfoot>
    1. <legend id='wtSfG'><style id='wtSfG'><dir id='wtSfG'><q id='wtSfG'></q></dir></style></legend>
    2. <small id='wtSfG'></small><noframes id='wtSfG'>

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

      Angular 2中的访问控制允许来源问题

      Access Control Allow Origin issue in Angular 2(Angular 2中的访问控制允许来源问题)
          <i id='R4Ln0'><tr id='R4Ln0'><dt id='R4Ln0'><q id='R4Ln0'><span id='R4Ln0'><b id='R4Ln0'><form id='R4Ln0'><ins id='R4Ln0'></ins><ul id='R4Ln0'></ul><sub id='R4Ln0'></sub></form><legend id='R4Ln0'></legend><bdo id='R4Ln0'><pre id='R4Ln0'><center id='R4Ln0'></center></pre></bdo></b><th id='R4Ln0'></th></span></q></dt></tr></i><div id='R4Ln0'><tfoot id='R4Ln0'></tfoot><dl id='R4Ln0'><fieldset id='R4Ln0'></fieldset></dl></div>
            <tbody id='R4Ln0'></tbody>
            <bdo id='R4Ln0'></bdo><ul id='R4Ln0'></ul>

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

                <tfoot id='R4Ln0'></tfoot>
                <legend id='R4Ln0'><style id='R4Ln0'><dir id='R4Ln0'><q id='R4Ln0'></q></dir></style></legend>
              • 本文介绍了Angular 2中的访问控制允许来源问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在从我的 node.js 服务器获取数据时遇到问题.

                客户端是:

                 public getTestLines() : Observable

                在服务器端我还设置了标题:

                resp.setHeader('Access-Control-Allow-Origin','*')resp.send(JSON.stringify(结果))

                但我得到一个错误

                <块引用>

                XMLHttpRequest 无法加载 http://localhost:3003/get_testlines.对预检请求的响应未通过访问控制检查:否请求中存在Access-Control-Allow-Origin"标头资源.因此,来源 'http://localhost:3000' 是不允许的访问."

                我该如何解决?当我删除标题时,它说这个标题是必需的.

                解决方案

                Access-Control-Allow-Originresponse 标头,而不是请求标头.

                您需要让它出现在响应中,而不是请求中.

                您已尝试将其放在响应中:

                <块引用>

                resp.setHeader('Access-Control-Allow-Origin','*')

                ……但它没有奏效.

                这可能是因为你没有把它放在对正确请求的响应中.错误消息说:

                <块引用>

                预检请求的响应未通过访问控制检查

                您已完成某事 以使请求预检.这意味着在浏览器发出您尝试发出的 GET 请求之前,它正在发出 OPTIONS 请求.

                这可能是由服务器上的另一段代码处理的,因此 resp.setHeader('Access-Control-Allow-Origin','*') 行不是被击中.

                导致发出预检请求的一件事是添加请求标头(除了少数例外).将 Access-Control-Allow-Origin 添加到 request 将触发预检请求,因此尝试解决问题的第一件事是 remove Access-Control-Allow-Origin 来自请求.

                如果失败,那么您需要设置您的服务器,以便它可以响应 OPTIONS 请求以及 GET 请求.

                I have a problem with getting data from my node.js server.

                The client side is:

                    public getTestLines() : Observable<TestLine[]> {
                    let headers = new Headers({ 'Access-Control-Allow-Origin': '*' });
                    let options = new RequestOptions({ headers: headers });
                
                    return this.http.get('http://localhost:3003/get_testlines', options)
                                .map((res:Response) => res.json())
                                .catch((error:any) => Observable.throw(error.json().error || 'Server error')); 
                }
                

                in server side I also set the headers:

                resp.setHeader('Access-Control-Allow-Origin','*') 
                resp.send(JSON.stringify(results))
                

                But I get an error

                "XMLHttpRequest cannot load http://localhost:3003/get_testlines. 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:3000' is therefore not allowed access."

                How can I fix it? When I remove headers it says that this header is required.

                解决方案

                Access-Control-Allow-Origin is a response header, not a request header.

                You need to have it appear on the response, not the request.

                You have attempted to put it on the response:

                resp.setHeader('Access-Control-Allow-Origin','*') 
                

                … but it hasn't worked.

                This is probably because you haven't put it on the response to the right request. The error message says:

                Response to preflight request doesn't pass access control check

                You have done something to make the request preflighted. This means that before the browser makes the GET request you are trying to make, it is making an OPTIONS request.

                This is, presumably, being handled by a different piece of code on your server so the line resp.setHeader('Access-Control-Allow-Origin','*') isn't being hit.

                One thing that causes a preflighted request to be made is the addition of request headers (other than a small number of exceptions). Adding Access-Control-Allow-Origin to the request will trigger a preflighted request, so the first thing to do to try to fix the problem is to remove Access-Control-Allow-Origin from the request.

                If that fails, then you need to set up your server so it can respond to the OPTIONS request as well as the GET request.

                这篇关于Angular 2中的访问控制允许来源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                What is quot;X-Content-Type-Options=nosniffquot;?(什么是“X-Content-Type-Options=nosniff?)
                SmtpJs API not working! is there any way to send emails using SMTP server with JavaScript or JQuery(SmtpJs API 不工作!有没有办法使用带有 JavaScript 或 JQuery 的 SMTP 服务器发送电子邮件)
                Can I send email using javascript(我可以使用 javascript 发送电子邮件吗)

                  <tbody id='4iAkm'></tbody>

                  <small id='4iAkm'></small><noframes id='4iAkm'>

                  • <bdo id='4iAkm'></bdo><ul id='4iAkm'></ul>

                      <tfoot id='4iAkm'></tfoot>

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