12

I am using the following code:

$http({
    method: 'GET',
    url: '/Admin/GetTestAccounts',
    data: { applicationId: 3 }
}).success(function (result) {
    $scope.testAccounts = result;
});

The code sends the following to my server:

http://127.0.0.1:81/Admin/GetTestAccounts

When this is received by my MVC controller:

[HttpGet]
public virtual ActionResult GetTestAccounts(int applicationId)
{
    var testAccounts =
        (
            from testAccount in this._testAccountService.GetTestAccounts(applicationId)
            select new
            {
                Id = testAccount.TestAccountId,
                Name = testAccount.Name
            }
        ).ToList();

    return Json(testAccounts, JsonRequestBehavior.AllowGet);
}

It complains that there is no applicationId.

The parameters dictionary contains a null entry for parameter 'applicationId' of non-nullable type 'System.Int32' for method

Can someone explain why the applicationId is not being sent as a parameter? Previously I was doing this with the following non-Angular code and it worked just fine:

$.ajax({
    url: '/Admin/GetTestAccounts',
    data: { applicationId: 3 },
    type: 'GET',
    success: function (data) {
        eViewModel.testAccounts(data);
    }
});
7
  • Does this help? stackoverflow.com/questions/10520174/… Commented Mar 29, 2013 at 16:23
  • @Dave - I saw the link you gave me and I am even more confused now :-( Commented Mar 29, 2013 at 16:36
  • look at url being used in browser console network tab.. what does it look like? Commented Mar 29, 2013 at 16:36
  • @Gemma have you tried using jQuery param $.param({ applicationId: 3 })? Commented Mar 29, 2013 at 17:52
  • why dont you append application id directly to url in query string Commented Mar 29, 2013 at 17:55

2 Answers 2

31

If you don't want to use jQuery's $.param you can use $http's param field which serializes an object.

var params = {
    applicationId: 3
}

$http({
    url: '/Admin/GetTestAccounts',
    method: 'GET',
    params: params
});
Sign up to request clarification or add additional context in comments.

5 Comments

The url encoding of the params cause trouble for me when Date objects are involved. The serialization pads the Date with the character '%22' (double-quote), which results in the modelbinder of WebAPI/MVC failing to deserialize the Date.
I've filed an issue on Github for this github.com/angular/angular.js/issues/8150
This is fixed in 1.3.16
This was not fixed for Moment.js. So be sure to use .toDate() if you use it.
3

Ok, I will try to answer this.

I think the problem is that angularjs presume that data passed to http will be urlencoded. I am not sure why angular doesn't serialize it implicitly if there's an object. So you have to encode it yourself:

 $http({
       method: 'GET',
       url: '/Admin/GetTestAccounts',
       data: 'applicationId=3'
       })

or use jQuery param to encode it for you:

$http({
     method: 'GET',
     url: '/Admin/GetTestAccounts',
     data: $.param({ applicationId: 3 })
     })

5 Comments

Based on their discussion of angular.extend() I would guess they don't like to implement deep copy and the like.
Equally, you could implement a shallow API query design or use JSON requests
You don't use data in a GET request, you use params
Getting Error: $ is not defined near $.param

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.