jsonp GET请求404错误

时间:2014-10-11 21:11:51

标签: javascript node.js angularjs socket.io jsonp

我正在使用Nodejs,Socket.io和Angular来利用Instagram Real-Time API构建一个Web应用程序。当我向Instagram API发出GET请求时,我遇到了一个问题。

每次发送GET请求时都会收到此错误:

"Failed to load resource: the server responded with a status of 404 (NOT FOUND)"

错误详情显示:

https://api.instagram.com/v1/geographies/[object%20Object]/media/recent?client_id=MY_CLIENT_ID

显然[object%20Object]是错误所在。正如下面的代码所示,那应该是我作为参数传递的'geo_id'。 Geo_id实际上是Instagram实时订阅的'object_id'。

以下是我的代码。知道我哪里错了吗?

Socket.io服务器端代码:

io.sockets.on('connection', function(socket) {
    // log user connections
    console.log("user connected");

    // receive the Instagram handshake for real-time subscriptions
    app.get('/callback', function(req, res){
        var handshake =  Instagram.subscriptions.handshake(req, res);
    });

    // for each new post Instagram sends us the data
    app.post('/callback', function(req, res) {
        var data = req.body;
        // grab the object_id (as geo_id) of the subscription and send as an argument   to the client side
        data.forEach(function(data) {
            var geo_id = data.object_id;
            sendUpdate(geo_id);
        });
        res.end();
    });

// send the url with the geo_id to the client side
// to do the ajax call
function sendUpdate(geo_id) {
    io.sockets.emit('newImage', { geo_id: geo_id });
}

    // log user disconnections
    socket.on('disconnect', function () {
        console.log('user disconnected');
    });
});

相关角度控制器代码:

socket.on('newImage', function(geo_id) {
    // pass geo_id into Instagram API call
    Instagram.get(geo_id).success(function(response) {
        instagramSuccess(response.geo_id, response);
    });

    // Instagram API callback
    var instagramSuccess = function(scope,res) {
        if (res.meta.code !== 200) {
            scope.error = res.meta.error_type + ' | ' + res.meta.error_message;
            return;
        }
        if (res.data.length > 0) {
            $scope.items = res.data;
        } else {
            scope.error = "This location has returned no results";
        }
    };
});

Instagram角度工厂代码:

angular.module('InstaFactory', []).factory('Instagram', function($http) {
    var base = "https://api.instagram.com/v1";
    var client_id = 'MY_CLIENT_ID';

    return {
        'get': function(geo_id) {
            var request = '/geographies/' + geo_id + '/media/recent?client_id=' + client_id;
            var url = base + request;
            var config = {
                'params': {
                    'callback': 'JSON_CALLBACK'
                }
            };
            return $http.jsonp(url, config);
        }
    };

});

0 个答案:

没有答案