0

I have the following

function quoteGridController($scope, $http) {
    $http.defaults.useXDomain = true;
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    delete $http.defaults.headers.common['X-Requested-With'];

    $http.post("http://localhost:57048/service/Quote/ReadQuoteForClientId", $.param({   "ClientId": 2 }))
    .success(function (data, status) {
        alert("Success");
        $scope.status = status;
        $scope.data = data;
    })
    .error(function (data, status) {
        alert("Error");
        $scope.status = status;
        $scope.data = data;
    });

}

This call fails when using Chrome but succeeds when using Internet Explorer. From what I have read this is a CORS problem. Is there any way I can get this call to work with both browsers?

EDIT: The Chrome console is giving me the following

XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Therefore not allowed access.

Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
  • What does your chrome console says? would you post that in your post above too. – Jai Aug 20 '14 at 07:35
  • 1
    It's not a client-only issue, as explained [here](http://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin). You'll have to modify the headers sent by your server as well (to allow CORS on localhost in Chrome). Here's [one possible approach](http://www.williamjohnbert.com/2013/06/allow-cors-with-localhost-in-chrome/). – raina77ow Aug 20 '14 at 07:39

1 Answers1

0

As mentioned in the comments above, this is not a client side issue. Chrome do not allow CORS when running from your localhost. A possible solution is to run your web API using a custom domain domain name as explained here.

I however took the easy route and started Chrome with web security disabled, just so that I can test my web API code. Run this from the command prompt

chrome.exe --disable-web-security

Then run your application

Community
  • 1
  • 1
Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96