Angular using $http and YQL



我不能在我的服务中使用angular $http service。我必须使用YQL进行JSON转换。$http不像jquery $.ajax吗?下面是我的代码:

    function cfgFunction($routeProvider)
{
    $routeProvider.
        when("/",{templateUrl:"partials/table.html"})
}
function testctrl($scope, $http) {
    var s = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%20%3D%20%22http%3A%2F%2Fapp.mytrading.it%3A8080%2FTS%2Fvis_dati.AllQuotes%3Fp_profilo%3D%26p_indice%3D%26p_id_cliente%3D%26p_nazione%3D%26p_num%3D%26p_titolo%3D%22&format=json&callback=';
  $http.get(s).success(function(data) {
      $scope.titoli = data.query.results.ROWSET.ROW;
  }).
  error(function(data, status, headers, config) {
    alert("error!")
    });
}
angular.module('MytApp',[]).config(cfgFunction);

我认为你应该使用jsonp:

$http.jsonp(url).success(...);

也不要忘记在你的查询中添加callback=JSON_CALLBACK

我有一个在jsFiddle上使用YQL的工作示例。

由于http是异步的,您有两个选项来处理它:

    使用$q返回承诺
  • 使用回调函数

尝试$http.jsonp(url,config),因为您正在向远程域发送请求。

最新更新