2015-08-20 97 views
0

我正在尝试使用$ http和get请求来调用api。我没有找到404,当我看到这个电话时,它正在追加http://localhost:3000/到前面这是问题。我怎样才能阻止它做到这一点?我正在使用角度和注入$ http。

app.factory('videos', ['$http', function($http){ 



    function pullData(){ 
    $http.get("api.redtube.com/data=redtube.Videos.searchVideos&output=json&search=hard&tags[]=Teen&thumbsize=medium").success(function(response){ 
    o.videos.push(response) 
    }); 
    } 

    pullData() // call the function to pull data from the API 

    var o = {videos: [] 

    }; 

    return o; 

}]) 
+0

当我包含http://在通话前我得到一个没有访问控制允许起源错误 – trebek1

+0

你可以在你提出请求的地方发布代码吗?似乎没有足够的信息。如果您提供完整的URL,如$ http({method:'GET',url:'https://www.example.com/api/v1/resource'}),$ http应该可以正常工作。 – GPicazo

+0

我添加了上面的代码,为了让它在浏览器中工作,我摆脱了http://但它仍然不起作用。这是网站api文档中的默认示例 – trebek1

回答

0

问题是url,而不是$ http提供程序。你错过了'?'字符表示数据是查询字符串的一部分。尝试使用这个:

app.factory('videos', ['$http', function($http){ 



function pullData(){ 
    $http.get("http://api.redtube.com/?data=redtube.Videos.searchVideos&output=json&search=hard&tags[]=Teen&thumbsize=medium").success(function(response){ 
    o.videos.push(response) 
    }); 
    } 

    pullData() // call the function to pull data from the API 

    var o = {videos: [] 

    }; 

    return o; 

}]) 

编辑:对于CORS问题,请尝试使用JSONP

// Instead of 
$http.get(...) 
// use 
$http.jsonp(...); 
+0

XMLHttpRequest无法加载http://api.redtube.com/?data=redtube.Videos.hardVideos标签[] = Teen&thumbsize = medium。请求的资源上没有“Access-Control-Allow-Origin”标题。原因'http:// localhost:3000'因此不被允许访问。 – trebek1

+0

Access-Control-Allow-Origin错误是一个CORS错误(与原始问题不同)。发生这种情况是因为您试图从一个域访问资源到另一个域。由于您只是在执行GET请求,因此请尝试使用JSONP,如上面编辑 – GPicazo

+0

所示,它表示未捕获的SyntaxError:意外的标记:。所以我想它不支持jsonp – trebek1