Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 返回JSONP时不执行HTTPpromise大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从雅虎的api获得股票报价,我正在使用angular的$http.jsonp方法.目标是当结果返回时,让应用程序转到这条路线:’/ stocks / show_stock’.我试图以两种方式做到这一点,但都不起作用
1)我发表声明:

window.LOCATIOn = '/stocks/show_stock'

在包装JSONP响应的回调函数
2)我发表声明:

$LOCATIOn.path '/stocks/show_stock'

httppromise回调中. (参见代码中的注释)

这是我的代码(在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)
  }

先感谢您.

解决方法

我不写coffeescript,所以我把它翻译成了javascript.你忘记注入$LOCATIOn服务,除了我刚刚将callBACk = stock_quote_callBACk替换为callBACk = JSON_CALLBACK并创建了一个运行得很好的plunker: http://run.plnkr.co/hCAdohIJIr9Odn3m/(来源: 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);
    }
  };
});

大佬总结

以上是大佬教程为你收集整理的angularjs – 返回JSONP时不执行HTTPpromise全部内容,希望文章能够帮你解决angularjs – 返回JSONP时不执行HTTPpromise所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。