CORS实现在Javascript中 [英] CORS implementation IN Javascript

查看:161
本文介绍了CORS实现在Javascript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要访问放置在其他服务器中的资源。我想从javascript访问它,我尝试实施CORS,请按照教程此处。但没有得到如何配置其他服务器与Access-Control-Allow-Origin头,所以仍然浏览器不允许访问。然后我尝试安装CORS扩展的铬,它的工作,但Chrome扩展的问题是,如果我访问任何资源从同一个服务器,网页被提升,我得到这个错误:访问控制 - Origin'头包含多个值,但只允许一个。起源 _ ,因此不允许访问。

I want to access a resource placed in someother server. I want to access it from javascript , I tried implementing CORS , by following tutorial here . But didn't get how to configure other server with Access-Control-Allow-Origin header, so still browser didnt allowed access . I then tried installing CORS extension for chrome , it worked , but the issue with chrome extension is that , if I access any resource from the same server , where web page is hoisted , I get this error : The 'Access-Control-Allow-Origin' header contains multiple values, but only one is allowed. Origin _ ,is therefore not allowed access.

如果Chrome扩展程序的跨域资源访问工作正常,但是来自同一域,则会失败。
我想通过代码实现CORS,而不是扩展。

In case of chrome extension cross domain resource access works but from same domain fails. I want to implement CORS through code , rather than extension .

推荐答案

有两种方法: / p>

There are two ways to do it :


  1. 将Chrome扩展程序CORS添加到chrome,然后如果您尝试创建跨网域XMLHTTPRequest,浏览器不会提示,它会加载跨域资源。

  1. Add the chrome extension CORS to chrome , then if you try to make a cross domain XMLHTTPRequest , the browser won't complain , it would load the cross domain resource.

如果你不想扩展,其他方式是配置跨域服务器。下面是wamp服务器的过程

If you dont want extension , other way is to configure the cross domain server.Below is the process for wamp server


  • 打开文件apache / conf / httpd.conf,并取消注释以下行:


LoadModule headers_module modules / mod_headers.so

LoadModule headers_module modules/mod_headers.so




  • 在目录标签下添加以下行。

    • Add below lines under "Directory" tag

      < IfModule mod_headers.c>
      标题集Access-Control-Allow-Origin*
      < / IfModule>

      使用*如果你想允许所有服务器的跨域,或明确给出服务器的名称。
      现在保存httpd.conf文件并重新启动wamp服务器。

      Use * if you want to allow cross domain from all servers else give the name of server explicitly. Now save the httpd.conf file and restart the wamp server.

      这篇关于CORS实现在Javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆