1

I am new to AngularJS. I have a .net MVC WebAPI Restful app running on a IIS server. When I query the api with http://xxx.xxx.xxx.xxx/api/project I get:

[{"Id":1,"Name":"Glenn Block","Created":"0001-01-01T00:00:00"},{"Id":2,"Name":"Dan Roth","Created":"0001-01-01T00:00:00"}]

I created a ProjectCtrl (in a separate empty project) that looks like this:

angular.module('Project', ['ngResource']);

function ProjectCtrl($scope, $resource) {
    $scope.project = $resource('http://192.168.1.221/api/project'
    );
    $scope.project.get(function (data) {
        console.log('success, got data: ', data);
    }, function (err) {
        alert('request failed');
    });
}

I always get a failure. I addressed CORS issues on the server and the request header contains

Access-Control-Request-He...    x-requested-with
Access-Control-Request-Me...    GET

What I find odd is that when I look in firebug it does NOT do a get but rather shows Option project with a status of 200

I am not sure what I missing.

4
  • 1
    Does the first parameter in the get need to be an empty {} object? If so, you (err) callback is actually your returned data. Just a guess. Commented Jul 9, 2013 at 16:40
  • I just tried this and no change. I did however, add these lines to the top of the ProjectCtrl...var myApp = angular.module('Project', ['ngResource']); myApp.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }]); and it now does a get and I see the data. I do get a error in console - Error: a.push is not a function Commented Jul 9, 2013 at 16:50
  • Bummer, I'll delete answer if it's wrong. Don't want this to go unanswered. Commented Jul 9, 2013 at 16:52
  • There were several parts to this answer and all the input was valuable in solving. dnc253 helped with the last piece regarding the query. The addition of the config setting were also required. Commented Jul 9, 2013 at 21:17

2 Answers 2

1

The error mentioned in your comment, "Error: a.push is not a function", is because your response is an array. With $resource, use the query function when your response is an array.

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

Comments

0

I think the first parameter of the resource call should be resource-based params. If you have none, pass an empty object {} as the first parameter:

$scope.project.get({},function (data) {
    console.log('success, got data: ', data);
}, function (err) {
    alert('request failed');
});

My guess is that your err object is actually the successfully returned data. If I'm wrong, tell me and I'll delete!

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.