角JSONP工厂无法正常工作 [英] angular jsonp factory doesn't work

查看:108
本文介绍了角JSONP工厂无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个厂的角度,它的返回我一个错误0 code一如既往,甚至寿我可以看到它从JSON原点取,有人可以帮我吗?

  app.factory('docFactory',函数($ HTTP){
  VAR docFactory = {
    异步:功能(页){
      VAR URL ='http://example.com/ProviderRequest?&queryString=searchv2&callback=JSON_CALLBACK';
        VAR承诺= $ http.jsonp(URL).error(功能(响应状态){
          警报(状态);
        })。成功(功能(响应状态){
          警报(状态);
        }),然后(功能(响应状态){
          返回response.data;
      });
      返回的承诺;
    }};
  返回docFactory;
});


解决方案

我遇到了这个自己。如果你的JSONP是一个函数调用或东西falsy(未定义),你会遇到这样的行为。看看 $ C从AngularJS HTTP后端$ C的更多信息。(行41 - 54相关)

如果您正在返回一个函数,你可能只需要调用后返回true或什么的。

修改:看你plunker后看来你JSONP响应不调用回调方法。该反应应该是 angular.callbacks._0({...}); ,其中 {...} 是你的对象,而 angular.callbacks._0 是请求的回调值查询参数。

I have this factory in angular, and its returning me a 0 error code always, even tho i can see it fetches from the json origin, can someone help me?

app.factory('docFactory', function($http) {  
  var docFactory = {      
    async: function(page) {
      var url = 'http://example.com/ProviderRequest?&queryString=searchv2&callback=JSON_CALLBACK';     
        var promise = $http.jsonp(url).error(function (response, status) {
          alert(status);
        }).success(function (response, status) {
          alert(status);
        }).then(function (response, status) {
          return response.data;           
      });
      return promise;
    }};
  return docFactory;
});

解决方案

I ran into this myself. If your JSONP is a function call or something falsy (null, false, undefined) you will encounter this behavior. Take a look at this code from AngularJS HTTP backend for more info (lines 41 - 54 are relevant).

If you are returning a function, you might just need to return true or something after the call.

Edit: After looking at your plunker it seems that your JSONP response is not calling the callback method. The response should be angular.callbacks._0( { ... } ); where { ... } is your object, and angular.callbacks._0 is the value of the callback query parameter in the request.

这篇关于角JSONP工厂无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆