这似乎是一个简单的问题,我必须忽略一些小问题。

我有一个访问Spotify API并搜索艺术家的函数。我知道通过普通URL访问此路由会返回结果。 (例如http://localhost:3001/search?artist=%27Linkin%20Park%27),执行此操作的代码如下:

router.get('/search', function(req, res, next)
{
    var artist = req.param('artist');
    console.log("Artist: " + artist);
    smartSpot.getArtistID(artist, function(data)
    {
        console.log("Data: " + data);
        res.json(data.id);
    });
});

然后,在前端有代码来搜索艺术家。这些都是通过 Angular 完成的。
angular.module('smart-spot', [])
    .controller('MainCtrl', [
        '$scope', '$http',
        function($scope, $http)
        {
            $scope.createPlaylist = function()
            {
                var artist = $scope.artist;
                console.log(artist);
                window.open("/login", "Playlist Creation", 'WIDTH=400, HEIGHT=500');
                return $http.get('/search?=' + $scope.artist) //this doesn't pass in the artist
                    .success(function(data)
                    {
                        console.log(data);
                    });

            }
        }
    ]);
$http.get()没有正确传递$ scope.artist`值。

最佳答案

看起来您可能在字符串连接中缺少“艺术家”查询参数。

$http.get('/search?artist=' + $scope.artist)

或者,您可以将艺术家作为查询参数传递。
function createPlaylist() {
    return $http.get('/search', { params : { artist : $scope.artist } })
    .then(function(response) {
        return response;
    }, function(error) {
        return $q.reject(error);
    });
}

另外,我将避免使用.success。我相信它已因上述语法而贬值。第一个参数是成功函数,第二个是失败函数。

关于javascript - Angular $ http.get不传递参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36538996/

10-12 00:40
查看更多