Angularjs Passing Data to $Http.Get Request

AngularJS passing data to $http.get request

An HTTP GET request can't contain data to be posted to the server. However, you can add a query string to the request.

angular.http provides an option for it called params.

$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});

See: http://docs.angularjs.org/api/ng.$http#get and https://docs.angularjs.org/api/ng/service/$http#usage (shows the params param)

Angular: Pass params to $http.get

The second argument of $http is a config object (see documentation). Amongst other properties, the config object accepts a params property:

  • params – {Object.<string|Object>} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

Therefore you have to pass the parameters as such

var config = {
params: {
one: value,
two: value
}
}

$http.get('/someUrl', config).then(...)

Suppose the values for the parameters are respectively '1' and '2', $http will send a GET request to the following url:

/someUrl?one=1&two=2

As a side note, try to avoid using success and error functions on $http. They have been deprecated as of angular 1.4.4. Use the methods then with a success and an error callback instead, or then with only a success callback and catch.

Pass and access parameters through AngularJS http GET

In your SQL query,you are GETtingdept and office.
You can pass these params as below.

you can use params : Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

$http({
method: 'GET',
url: 'angular-data.php',
params: 'dept=some_dept, office=some_office'
})

OR

$http({
method: 'GET',
url: 'angular-data.php',
params: {
dept:"some_dept",
office:"some_office"
}
})

More can be find here $http#config

How to pass variable from $http success to another $http request in Angularjs?

Simply chain the two XHRs:

function getOSChild (x) {
return $http({
method: "GET",
url: url+'getOSchild',
params: {ncard: x}
}).then(function success(response) {
$scope.osChild = response.data;
console.log($scope.osChild); // this has an output
return response.data;
},function error(response) {
console.log(response)
console.log(response.status);
throw response;
});
}

$scope.submit = function(x) {
getOSChild(x).then(function(osChild) {
$http({
method: "POST",
url: url+'printOS',
data:{ CARD_NAME: data_cname,
C_DATE: data_date,
C_NUMATCARD: data_ncard,
C_DISTMEANS: data_means,
C_TIME: data_time,
cData: osChild //chained

}
}).then(function success(response) {
console.log(response)
});
});
};

The .then method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining.

For more information, see

  • AngularJS $q Service API Reference - chaining promises
  • You're Missing the Point of Promises

How to do a $http GET with some data in AngularJS?

You're most likely going to need to use a POST method instead of a GET method.

But to do it with a GET method:

From AngularJS passing data to $http.get request:

A HTTP GET request can't contain data to be posted to the server.
However you can add a query string to the request.

angular.http provides an option for it params.

$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});

And using AngularJS to POST instead:

$http.post('/someUrl', {msg:'hello word!'}).
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});

AngularJS with WebAPI pass JSON parameters to $http.get

Update:

Problem is solved by changing GET back to POST.

I had wrongly assumed that I was supposed to use GET



Related Topics



Leave a reply



Submit