madimper
madimper

Reputation: 67

Angular using $http and YQL

I can't use angular $http service for my services. I have to use YQL for JSON conversion. Isn't $http like jquery $.ajax? Here is my code:

    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);

Upvotes: 3

Views: 2037

Answers (2)

asgoth
asgoth

Reputation: 35829

In my opinion you should use jsonp:

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

Also dont forget to add callback=JSON_CALLBACK to your query.

I have a working example using YQL on my jsFiddle.

Since http is asynchronuous, you have two options to process it:

  • with returning a promise by using $q
  • with using a callback function

Both are shown in the example.

Upvotes: 6

Mahbub
Mahbub

Reputation: 3118

Try $http.jsonp(url,config) since you're sending request to a remote domain.

Upvotes: 0

Related Questions