$ http get参数不起作用

时间:2013-06-20 22:45:18

标签: javascript angularjs http angular-http

有谁知道为什么这不起作用?

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

这确实有效:

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

2 个答案:

答案 0 :(得分:191)

get调用中的第二个参数是配置对象。你想要这样的东西:

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

请参阅http://docs.angularjs.org/api/ng.$http参数部分了解更多详情

答案 1 :(得分:3)

$http.get docs开始,第二个参数是配置对象:

  

get(url, [config]);

     

执行GET请求的快捷方式。

您可以将代码更改为:

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

或者:

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

作为旁注,由于 Angular 1.6 .success should not be used anymore,请改用.then

$http.get('/url', config).then(successCallback, errorCallback);