Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get a stock quote from yahoo's api and I am using angular's $http.jsonp method. The goal is when the result comes back, to have the app go to the this route: '/stocks/show_stock'. I am trying to do it in two ways and neither works 1) I put the statement:

window.location = '/stocks/show_stock'

in the callback function that wraps the JSONP response 2) I put the statement:

$location.path '/stocks/show_stock'

in the HTTPpromise callback. (see the comments in the code)

Here is my code (in coffescript):

#THIS IS THE CALLBACK FUNCTION THAT I SEND WITH THE JSONP REQUEST  
window.stock_quote_callback = (data)->
  console.log data #THIS WORKS AND I CAN SEE THE DATA RETURNED FROM YAHOO
  window.stock_quote_result = data.results
  alert 'I am in the callback'
  #THE STATEMENT BELOW DOES NOT WORK EVEN THOUGH I CAN SEE THE ALERT ABOVE
  window.location = '/stocks/show_stock'

angular.module('Services').service 'StockSupplier', ($http)->
  get_stock = (symbol)->
    q = 'select * from yahoo.finance.quotes 
        where symbol in ("'+symbol+'")
        &format=json&
        diagnostics=true&
        env=http://datatables.org/alltables.env&
        callback=stock_quote_callback'
    url = 'http://query.yahooapis.com/v1/public/yql?q='+q 
    $http.jsonp(url).then (data)->
      #THE CODE BELOW NEVER EXECUTES EVEN THOUGH RESULT IS RETURNED
      alert 'This should pop up when result returns'
      $location.path'/stocks/show_stock'

  {
    get_stock: (symbol)-> get_stock(symbol)
  }

Thank you in advance.

share|improve this question
    
you have not added callback=JSON_CALLBACK to the url, the url should be query.yahooapis.com/v1/public/… –  Ajay Beniwal May 25 '13 at 17:31
1  
it is there. the 6th line of the 'q' string. As I mentioned that is not the problem I do get the callback to fire but window.location = '/stocks/show_stock' inside that callback does not work. That is why I tried to use the 'then' function of httpPromise but that didn't work either –  dimitar May 25 '13 at 17:39
    
Thanks Ajay now I know what you mean. –  dimitar May 25 '13 at 19:37

1 Answer 1

up vote 1 down vote accepted

I don't write coffeescript, so I translated it to javascript. You forgot to inject the $location service, other than that I just replaced the callback=stock_quote_callback to callback=JSON_CALLBACK and created a plunker that runs just fine: http://run.plnkr.co/hCAdohIJIr9Odn3m/ (source: http://plnkr.co/edit/a7C6k0QVoXnaTyImSUkb?p=preview).

angular.module('Services').service('StockSupplier', function($http,$location) {
  var get_stock;
  get_stock = function(symbol) {
    var q, url;
    q = 'select * from yahoo.finance.quotes where symbol in ("' + symbol + '")&'+
        'format=json&'+
        'diagnostics=true&'+
        'env=http://datatables.org/alltables.env&'+
        'callback=JSON_CALLBACK ';
    url = 'http://query.yahooapis.com/v1/public/yql?q=' + q;
    return $http.jsonp(url).then(function(data) {
      alert('This should pop up when result returns');
      $location.path( '/stocks/show_stock' );
    });
  };
  return {
    get_stock: function(symbol) {
      return get_stock(symbol);
    }
  };
});
share|improve this answer
    
Thanks a bunch jo! Reading the angular documentation I thought that 'JSON_CALLBACK' was a placeholder for the name of the function you want executed when the response returned, and not that I have to literally put 'JSON_CALLBACK' as the name of the function. That solved it. –  dimitar May 25 '13 at 19:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.