MVC CORS works in IE but not in Chrome

1.2k Views Asked by At

I'm stuck with a CORS issue which works totally fine when I'm using Internet Explorer, but doesn't work with Google Chrome.

I have 2 separate projects in Visual Studio 2013: PROJECT 1 is on port 1044, it's just an empty project containing an HTML page with a Button which uses AngularJS to make a call to ACTION GetCustomer residing inside PROJECT 2 on port 1042. The ACTION then returns JSON data back to PROJECT 1.

The cross domain call works fine when the Button is clicked in IE and returns the data back to the HTML TextBox saying "ShivDataFromServer". But the same doesn't happen in Chrome.

PROJECT 1 on port 1044:

HomePage.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<script src="angular.min.js"></script>
<!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>-->
<body>
    <script type="text/javascript" src="CustomerViewModel.js"></script>
    <div ng-app>
        <!-- Below is our VIEW i.e. Display-->
        <div id="ViewCustomer" ng-controller="MyController">
            Customer Name: <input type="text" id="txtCustomerName" ng-model="Customer.Name" /> <br />
            Customer Amount: <input type="text" id="txtCustomerAmount" ng-model="Customer.Amount" /> <br />

            <div style="width : 242px; height : 26px; background-color : {{CustomerView.Color}}"></div> <br />
            <input type="button" id="btn1" value="Get data from Server" ng-click="GetData()" />
        </div>
    </div>
</body>
</html>

CustomerViewModel.js:

function MyController($scope, $http) {
    $scope.Customer = { "Name": "ShivHardCodedData", "Amount": "1000" };

    $scope.CustomerView = { "Color": "" };

    //BELOW IS TRANSFORMATION LOGIC! Depending on Amount it will be GREEN or RED meaning "danger".
    $scope.$watch("Customer.Amount", function() {
            if ($scope.Customer.Amount < 1000) {
                $scope.CustomerView.Color = "Green";
            } else {
                $scope.CustomerView.Color = "Red";
            }
        }
    );

    $scope.GetData = function () {
        //BELOW WORKS!!
        $http({ method: "GET", url: "http://localhost:1042/Customer/GetCustomer" })
            .success(function (data, status, headers, config) { $scope.Customer = data; })
            .error(function (data, status, headers, config) { });

    } //END OF "GetData() function"
}   

PROJECT 2 which is an MVC project on port 1042:

CustomerController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using P10JsonJQuery_httpService.Models;

namespace P10JsonJQuery_httpService.Controllers
{
    public class CustomerController : Controller
    {
        [ImAllowingCors]
        public ActionResult GetCustomer()
        {
            Customer objCustomer=new Customer();
            objCustomer.Name = "ShivDataFromServer";
            objCustomer.Amount = 1000;
            return Json(objCustomer, JsonRequestBehavior.AllowGet);
        }
    }

    //CORS (Cross Origin Resource Sharing). I've made up name "ImAllowingCors" but ending "Attribute" is C# KEYWORD
    public class ImAllowingCorsAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //This means ALLOW any calls from a Cross-domain (i.e. allow calls from different server)
            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");

            base.OnActionExecuting(filterContext);
        }
    }
}
1

There are 1 best solutions below

1
On

ie does not count calls on different ports, but the same domain as being cross origin, whereas Chrome does. Thus, you need to set it up for chrome to allow this.

To do it the way you are trying, you will need to make sure the custom filter is registered and I think you will also need to change your attribute to [ImAllowingCorsAttribute]: See: similar issue

You will need to allow Cors for your app. Something like the following annotation above the action:

[EnableCors(origins: "http://localhost:1042", headers: "*", methods: "*")]

Perhaps put a breakpoint in your attribute, as I doubt it is being hit. This should show you how to enable cors: enabling Cors