1

I've got a wcf Service which has a method which send its output in jsonformat. The service is hosted in an https-Environment.

i'm calling it with angularjs-resource:

var hrdemo = angular.module('hrdemo', ["ngResource"]);

hrdemo.controller('HrDemoCtrl', function ($scope, hrdbservice) {
    $scope.items = hrdbservice.get({ 'Id': 1 });
    var a = $scope.items.length;
});

hrdemo.factory('hrdbservice', function ($resource) {
    return $resource('http://hrservice/HrService.svc/:Id', { Id: "@Id" }, { get: { method: 'JSONP' } });
});

Angularjs runs in an ASP.Net-Web Application. When calling the Service I get something like an xhr-problem.

1) How can i authenticate with my Windows authentication over angularjs 2) What can I do to fix the xhr-problem?

  • what is the error ? can you run the webservice from browser by typing the url in address bar? – Michael B. Aug 20 '14 at 09:31
  • the error happens when using the Standard get without the method "JSONP" in the $resource-call, Error: Exception was thrown at line 8473, column 7 in https://localhost:44304/Scripts/angular.js 0x80070005 - JavaScript runtime error: Access is denied. – Steffen Schindler Aug 20 '14 at 09:52
  • the Service works when using the browser – Steffen Schindler Aug 20 '14 at 09:53
  • why jsonp? are you doing cross-domain request ? – Michael B. Aug 20 '14 at 09:56
  • Yes, my WCF service is on another machine. – Steffen Schindler Aug 20 '14 at 10:04
  • Then you have to use jsonp? that solve issue # 2, right? for #1, on code server you need to have the IIS App pool identity to be an windows account that have access to the other WCF server (ex: domain account) – Michael B. Aug 20 '14 at 10:09
  • Yes that solves issue #2. Thanks (I was not sure why use jsonp, thanks for that). The Service is hosted in IIS (own hostheader) and the application is used in IIS Express (in development/ localhost). They are both on the same machine but will be on different machines in the future. – Steffen Schindler Aug 20 '14 at 10:12
  • because both are on the same machine the app pool identity can Access the wcf Service. – Steffen Schindler Aug 20 '14 at 10:13
  • Is your WebService capable of producing JSONP ? [Have a look at this answer.](http://stackoverflow.com/a/14236513/1540688) – Mx. Aug 20 '14 at 14:00
  • Yes it can produce jsonp. I played a bit with fiddler and i get an 401. I think all I Need to do is to pass the Windows authentication I got in the application with angularjs to the wcf Service. but i don't know how. – Steffen Schindler Aug 20 '14 at 14:13
  • May the `withCredentials` option is what you are searching for `$http.post(url, {withCredentials: true, ...})`. Also there is a shortcut for performing a JSONP request in AngularJS - `$http.jsonp()` [Source](https://docs.angularjs.org/api/ng/service/$http) – Mx. Aug 20 '14 at 14:40
  • withcredentials worked. Thanks. If you make your comment an answer i will mark it as correct answer. – Steffen Schindler Aug 20 '14 at 15:27

1 Answers1

2

May the withCredentials option is what you are searching for $http.post(url, {withCredentials: true, ...}). Also there is a specific shortcut for performing a JSONP request in AngularJS - $http.jsonp()

AngularJS Docs

Mx.
  • 3,588
  • 2
  • 25
  • 33