角休止不能返回简单的数据 [英] Angular REST cannot return simple data

查看:186
本文介绍了角休止不能返回简单的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个$ HTTP REST GET调用在我Appgyver项目工作的工作,但我没有这样做,似乎来的权利,总是会返回一个错误。

请注意角应用将在移动设备最终被运行,然后连接到我的远程Web服务。

我双重检查,我的定制的API是工作,在许多方面正确地返回数据,即:


  • $很难在终端SH文件运行C $ CD cUrl作者要求 - 返回的数据,正确的200 code

  • 测试的API终点两个邮递员,Firefox的SOA客户端。

http://somesite.com/quote我的测试终点推杆 - 和买性能/ druidapi / taxonomy_term 如下返回数据:

  [{TID:1,VID:2,名:ACME Ltd.\",\"description\":\"\",\"format\":\"filtered_html\",\"weight\":\"0\",\"parent\":\"0\",\"uri\":\"http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term/1\"},{\"tid\":\"2\",\"vid\":\"2\",\"name\":\"ABC电影LTD\",\"description\":\"\",\"format\":\"filtered_html\",\"weight\":\"0\",\"parent\":\"0\",\"uri\":\"http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term/2\"}]

即使是一个简单的CSRF令牌的请求给我的错误。

可能有人可能指出哪里我在这里会错了,Appgyver部位被严重记载和我已经试过这我下面code基于从的 https://docs.angularjs.org/api/ng/service/ $ http和的 https://docs.angularjs.org/api/ng/service/ $#HTTP设置-HTTP报头。

请注意下面的code基本上Angular.js使用JavaScript语法(而不是CoffeeScript的),日志输出如下code

 
.module('主')
.controller('的LoginController',函数($范围,超音速,$ HTTP){
    $ scope.navbarTitle =设置;    $ scope.stoken =响应到这里;    $ scope.processLogin =功能(){
        VAR csrfToken;        steroids.logger.log(START CALL:processLogin);        // $ form_login_email_address = $ scope.login_email;
        // $ form_login_password = $ scope.login_password;        $ local_get =htt​​p://somesite.com/quote-and-buy-performance/services/session/token;
        $ hal_get_taxterm_index =htt​​p://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term;        // $ http.defaults.headers.common.contentType(应用/ JSON');        VAR REQ = {
            方法:GET,
            网址:$ hal_get_taxterm_index,
            标题:{
                内容类型:应用/ JSON
            }
        }        $ HTTP(REQ)
          .success(功能(数据,状态,标题){
            steroids.logger.log(内部http.get()成功);
        })错误(功能(数据,状态,标题){
            steroids.logger.log(内部http.get()显示错误);
            steroids.logger.log('数据:'+数据);
            steroids.logger.log('状态:'+状态);
        }),然后(功能(数据,状态,标题){
            steroids.logger.log(内部http.get(),则);
        });        steroids.logger.log(END CALL:processLogin);
    }});

从通话记录输出到steroids.logger.log

 查看时间级别消息
主#登录16:01:55.219信息内部http.get()显示错误
主#登录16:01:55.219信息数据:空
主#登录16:01:55.219的信息状态:0
主#登录16:01:55.64的信息END CALL:processLogin
主#登录16:01:55.64资讯开始呼叫:processLogin


解决方案

下面是我会做:

分离出你的HTTP调用到服务。这是在模块化的角度code一pretty标准方式:

  angular.module(主)。工厂(SomeService功能($ HTTP){
  返回{
    得到:函数(){
      $ HTTP({
        网址:http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term
        方法:GET,
        标题:{
          内容类型:应用/ JSON
        }
      })。成功(功能(数据,状态,头,配置){
        的console.log(成功!);
        的console.log(数据);
      })错误(功能(数据,状态,头,配置){
        的console.log(错误!);
        的console.log(状态);
        的console.log(数据);
      });
    }
  }
})

然后在你的控制器使用,只需将其包含在你的控制器声明,并调用get就像您正常的方法:

  angular.module(主)。控制器('的LoginController',函数($范围,超音速,SomeService){
  $ scope.navbarTitle =设置;
  $ scope.stoken =响应到这里;
  $ scope.processLogin =功能(){
    VAR csrfToken;
    steroids.logger.log(START CALL:processLogin);    SomeService.get();    steroids.logger.log(END CALL:processLogin);
  }
})

做到这一点,然后注释回到你的结果,我们可以从那里工作。

I am trying to get a $http REST GET call working in my Appgyver project working but nothing I do seems to come right, always returns an error.

Please note the angular app will be running on mobile devices eventually and then connect to my remote web service.

I've double checked that my custom API is working and returning data correctly in a number of ways, namely:

  • hard coded cUrl request running from sh files in terminal - Returns data and correct 200 code
  • Tested the API end points in both POSTMAN and Firefox's SOA client.

Putting in my test end point of http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term returns data as below:

[{"tid":"1","vid":"2","name":"ACME Ltd.","description":"","format":"filtered_html","weight":"0","parent":"0","uri":"http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term/1"},{"tid":"2","vid":"2","name":"ABC Films LTD","description":"","format":"filtered_html","weight":"0","parent":"0","uri":"http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term/2"}]

Even a simple CSRF Token request gives me errors.

Could someone possibly point out where I am going wrong here, the Appgyver site is badly documented and I have tried the Angular RESTful sample which my code below is based upon from https://docs.angularjs.org/api/ng/service/$http and https://docs.angularjs.org/api/ng/service/$http#setting-http-headers

Please note the code below is basically Angular.js using Javascript syntax (as opposed to Coffeescript), logging output follows the code

angular
.module('main')
.controller('LoginController', function($scope, supersonic, $http) {
    $scope.navbarTitle = "Settings";

    $scope.stoken = "Response goes here";

    $scope.processLogin = function(){
        var csrfToken;

        steroids.logger.log("START CALL: processLogin");

        // $form_login_email_address = $scope.login_email;
        // $form_login_password = $scope.login_password;

        $local_get = "http://somesite.com/quote-and-buy-performance/services/session/token";
        $hal_get_taxterm_index = "http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term";

        // $http.defaults.headers.common.contentType('application/json');

        var req = {
            method: 'GET',
            url: $hal_get_taxterm_index,
            headers: {
                'Content-Type': 'application/json'
            }
        }

        $http(req)
          .success(function(data, status, headers) {
            steroids.logger.log("Inside http.get() success");
        }).error(function(data, status, headers){
            steroids.logger.log("Inside http.get() WITH ERROR");
            steroids.logger.log('data: ' + data);
            steroids.logger.log('status: ' + status);
        }).then(function(data, status, headers){
            steroids.logger.log("Inside http.get() then");
        });

        steroids.logger.log("END CALL: processLogin");
    }

});

Logging output from calls to steroids.logger.log

View        Time            Level   Message
main#login  16:01:55.219    info    "Inside http.get() WITH ERROR"
main#login  16:01:55.219    info    "data: null"
main#login  16:01:55.219    info    "status: 0"
main#login  16:01:55.64     info    "END CALL: processLogin"
main#login  16:01:55.64     info    "START CALL: processLogin"

解决方案

Here's what I would do:

Separate out your http call into a service. This is a pretty standard way to modularize your code in angular:

angular.module('main').factory("SomeService", function($http) {
  return {
    get: function() {
      $http({
        url: "http://somesite.com/quote-and-buy-performance/druidapi/taxonomy_term",
        method: "GET",
        headers: {
          "Content-Type": "application/json"
        }
      }).success(function(data, status, headers, config) {
        console.log("Success!");
        console.log(data);
      }).error(function(data, status, headers, config) {
        console.log("Error!");
        console.log(status);
        console.log(data);
      });
    }
  }
})

Then to use this in your controller, just include it in your controller declaration and call get like you would a normal method:

angular.module('main').controller('LoginController', function($scope, supersonic, SomeService) {
  $scope.navbarTitle = "Settings";
  $scope.stoken = "Response goes here";
  $scope.processLogin = function(){
    var csrfToken;
    steroids.logger.log("START CALL: processLogin");

    SomeService.get();

    steroids.logger.log("END CALL: processLogin");
  }
})

Do this and then comment back with your results and we can work from there.

这篇关于角休止不能返回简单的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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