Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send parameters in my http request using AngularJS?

Tags:

angularjs

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);
    }
});
like image 388
Alan2 Avatar asked Mar 29 '13 16:03

Alan2


People also ask

How do you pass multiple parameters in HTTP GET request in angular 8?

Passing multiple parameters to Http get request We have to pass page & per_page parameters to the list of users API. let queryParams = new HttpParams(); queryParams = queryParams. append("page",1); queryParams = queryParams. append("per_page",1);

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

Can you send data with HTTP GET?

Can I send data using the HTTP GET method? No, HTTP GET requests cannot have a message body. But you still can send data to the server using the URL parameters. In this case, you are limited to the maximum size of the URL, which is about 2000 characters (depends on the browser).


2 Answers

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
});
like image 167
Lucas Raines Avatar answered Oct 26 '22 06:10

Lucas Raines


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 })
     })
like image 28
davekr Avatar answered Oct 26 '22 06:10

davekr