javascript - AngularC# CORS Issue - Stack Overflow

I hosted my client on localhost:8080 and server on localhost:44302I am trying to link to my backend,

I hosted my client on localhost:8080/ and server on localhost:44302/

I am trying to link to my backend, but I am getting CORS issue. Below is my Angular http request

$http.post(url, data, {
    headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Methods': 'POST, OPTIONS'
             }
}).success(function (response) {
  // rest of the code
}).error(function (err, status) {
  // rest of the code
});

On the server side which is written in C#, I have the following set on the response

 Response.ContentType = "application/json";
 Response.AddHeader("Access-Control-Allow-Origin", "*");
 Response.AddHeader("Access-Control-Allow-Methods", "POST, OPTIONS");

Even after setting these, I am getting the following error

XMLHttpRequest cannot load https://localhost:44302/some_uri. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.

What am I missing?

I hosted my client on localhost:8080/ and server on localhost:44302/

I am trying to link to my backend, but I am getting CORS issue. Below is my Angular http request

$http.post(url, data, {
    headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Access-Control-Allow-Origin': '*',
                    'Access-Control-Allow-Methods': 'POST, OPTIONS'
             }
}).success(function (response) {
  // rest of the code
}).error(function (err, status) {
  // rest of the code
});

On the server side which is written in C#, I have the following set on the response

 Response.ContentType = "application/json";
 Response.AddHeader("Access-Control-Allow-Origin", "*");
 Response.AddHeader("Access-Control-Allow-Methods", "POST, OPTIONS");

Even after setting these, I am getting the following error

XMLHttpRequest cannot load https://localhost:44302/some_uri. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.

What am I missing?

Share Improve this question asked Jun 8, 2015 at 22:43 Abhijith NagarajaAbhijith Nagaraja 3,3806 gold badges30 silver badges57 bronze badges 2
  • 1 What are you using for backend? ASP.NET Web API? Where did you put the code that modifies the response? – SoftwareFactor Commented Jun 8, 2015 at 22:49
  • Yes am using ASP.NET MVC, I put the code in the controller from where I am sending the response back – Abhijith Nagaraja Commented Jun 8, 2015 at 22:51
Add a ment  | 

2 Answers 2

Reset to default 3

You do not need to send any additional headers from AngularJS. Preflight request will be made for you automatically.

To enable all cross-origin requests in Web API, you can do this:

  1. Install NuGet package Microsoft.AspNet.WebApi.Cors.
  2. Add the following code to WebApiConfig.cs:

        var corsAttr = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(corsAttr);
    

Full understanding of CORS is worth a few minutes of time. I remend reading an in-depth tutorial here:

http://www.asp/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Perform this in your application level server side No changes required in your client-side application

Step 1: Install-Package Microsoft.AspNet.WebApi.Cors

Step 2: From HttpConfiguration object you can enable cors - Applicable for the entire application

public static void Register(HttpConfiguration config)
    {
        // New code
        config.EnableCors();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

Alternate :

Step 3: Go to controller add this below attribute - Applicable only to TestController

// Allow CORS for all origins. (Caution!)
[EnableCors(origins: "*", headers: "*", methods: "*")]

Example :

[EnableCors(origins: "http://mywebclient.azurewebsites", headers: "*", methods: "*")]
public class TestController : ApiController
{
    // Controller methods not shown...
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249059a4618563.html

相关推荐

  • javascript - AngularC# CORS Issue - Stack Overflow

    I hosted my client on localhost:8080 and server on localhost:44302I am trying to link to my backend,

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信