$http get parameters does not work

JavascriptAngularjsHttpAngular Http

Javascript Problem Overview


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
    });

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkahonmlgView Question on Stackoverflow
Solution 1 - Javascriptdnc253View Answer on Stackoverflow
Solution 2 - JavascriptMistalisView Answer on Stackoverflow