2
  1. List item

I have a web API deployed on 'https://example.com/api/gd/alldata', when i run it directly from browser URL it shows the result. but when i call it from other Angular JS application, it is giving me below error.

Error:

Failed to load 'https://example.com/api/gd/alldata' The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:64000, http://localhost:64000, *', but only one is allowed. Origin 'http://localhost:64000' is therefore not allowed access.

This is my new angularjs service in another application.

  var promise = $http({

        method: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "https://example.com/api/gd/alldata",
        //headers: headers, 
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json',
        }
    })
        .then(function (response) {
            console.log(response.data)
            return response.data;
        },
        function (response) {
                return response.data;
            });

Below is my API code already deployed on some link,

i enabled cors in webapiconfig and entry on startup.cs file.

Startup.cs file

    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

webapiconfig.cs

 config.EnableCors();

Can you please tell me where i am doing mistake or anything i am missing or doing wrong in my code.

6
  • Where are you hosting your API? Commented Sep 7, 2018 at 9:39
  • on azurewebsites.net Commented Sep 7, 2018 at 9:41
  • Did you add the CORS setting on your azure's app service? Commented Sep 7, 2018 at 9:53
  • no i didnt add the CORS settings on azure. i just added the cors in my website service and deployed it on azure. Commented Sep 7, 2018 at 10:02
  • Check my answer below. Commented Sep 7, 2018 at 10:02

1 Answer 1

3

Since you are hosting on Azure and calling from localhost (local dev environment), you will have to update the following CORS setting on Azure App Service:

  1. Goto your App Service on https://portal.azure.com
  2. Search for CORS (press CTRL + /)
  3. Add your localhost:6400 or better yet * under Allowed Origins
  4. Save

enter image description here

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

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.