0

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.

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 .

3
  • does the other server also has the CORS header in the response it sends ? you can check that from Chrome's developer tools Network tab. Commented Jul 11, 2014 at 5:38
  • In javascript I have made the required CORSrequest , but on other server there is wamp , I am not able to figure out where to add below line : Header set Access-Control-Allow-Origin "*" . is it httpd.conf or any other file , I have added it in <Directory> but still when I try request , I get error : No 'Access-Control-Allow-Origin' header is present on the requested resource. Commented Jul 11, 2014 at 11:17
  • By using CORS extension for chrome , Without making any configuration changes to other server , I get response . Commented Jul 11, 2014 at 11:20

2 Answers 2

1

There are two ways to do it :

  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.

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

    • open file apache/conf/httpd.conf and uncomment below line :

    LoadModule headers_module modules/mod_headers.so

    • Add below lines under "Directory" tag

      <IfModule mod_headers.c> Header set Access-Control-Allow-Origin "*" </IfModule>

    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.

Sign up to request clarification or add additional context in comments.

Comments

0

Besides the two opetions mentioned by @Bhupendra, You could also run a proxy. The Proxy goes between your browser and the actual server, and adds the headers on the fly. For development, this is a perfectly fine solution.

Two node projects that i have used:

You will need node.js and npm for both.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.