Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

I want to make a Get request to my Backend, but I want to verify some user credentials before sending the response object.

Here is my code:

$scope.getEverything = function (){
    $http.get('http://localhost:1234/things').success(function(data){
        $scope.things = data;
    });
};

I've tried the same code with the following syntax:

$http.method('path).then
$http.method(config)

And I just could not figure out how to pass in some data. Trying to use params:data as the config object with print out:

GET http://localhost:1234/[object%20Object] 404 (Not Found)

On the console.

The AngularJS Docs don't say a word about sending data with GET requests. I've also tried some answers of Stack, but most of then are outdated and just won't work.

Can I send data with GET request or this is just one design error of my application?

like image 962
Rodmentou Avatar asked Aug 24 '15 20:08

Rodmentou


People also ask

How do we pass data and get data using http in angular?

get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });

What is $HTTP in AngularJS?

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

How do you modify the $HTTP request default Behaviour?

To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults.

How send parameters in HTTP GET request?

To use this function you just need to create two NameValueCollections holding your parameters and request headers. Show activity on this post. You can also pass value directly via URL. If you want to call method public static void calling(string name){....}


2 Answers

For $http.get requests use the params property of the config object

$http.get(url, {params:{ foo:'bar'}}).then(func....

See the Arguments table in Usage section of $http docs

like image 192
charlietfl Avatar answered Sep 21 '22 02:09

charlietfl


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.
  });
like image 42
Wyetro Avatar answered Sep 21 '22 02:09

Wyetro