$http get parameters does not work

113,839

Solution 1

The 2nd parameter in the get call is a config object. You want something like this:

$http
    .get('accept.php', {
        params: {
            source: link,
            category_id: category
        }
     })
     .success(function (data,status) {
          $scope.info_show = data
     });

See the Arguments section of http://docs.angularjs.org/api/ng.$http for more detail

Solution 2

From $http.get docs, the second parameter is a configuration object:

get(url, [config]);

Shortcut method to perform GET request.

You may change your code to:

$http.get('accept.php', {
    params: {
        source: link, 
        category_id: category
    }
});

Or:

$http({
    url: 'accept.php', 
    method: 'GET',
    params: { 
        source: link, 
        category_id: category
    }
});

As a side note, since Angular 1.6: .success should not be used anymore, use .then instead:

$http.get('/url', config).then(successCallback, errorCallback);
Share:
113,839
kahonmlg
Author by

kahonmlg

I am a interested person for programming. I like explore new ways of programming, new libraries and of course I like javascript so much. Even that I have programmed in several languages as a Java, C#, Python, C++, QT and QML. I am an adaptable programmer and well problems resolver. PD: I am an artist I paint canvas.

Updated on May 23, 2020

Comments

  • kahonmlg
    kahonmlg almost 4 years

    Does anyone know why this does not work?

    $http
        .get('accept.php', {
            source: link,
            category_id: category
        })
        .success(function (data, status) {
            $scope.info_show = data
        });
    

    and this does work:

    $http
        .get('accept.php?source=' + link + '&category_id=' + category)
        .success(function (data, status) {
            $scope.info_show = data
        });
    
  • DanielM
    DanielM about 9 years
    Note, using params as above also resolves the problem of GET requests not using data. AngularJS will not sort this out itself as jQuery does. (I don't think that's a good or bad thing, just different and may trip people up).
  • Winnemucca
    Winnemucca over 8 years
    I am getting undefined for my key value properties inside of the params object. Should this be different in a service?
  • nfang
    nfang over 8 years
    Also, when the params object is empty, or all its properties are null or undefined, nothing will be added to the query string.