如何从指令控制器中生成$ http.get请求?

 王之玉58_913 发布于 2023-01-08 08:10

我有一个名为product的AngularJS指令.每当我点击一个按钮时,我就需要采取行动,该按钮是该指令的一部分,涉及向我的服务器发出$ http.get请求并对响应进行处理.我有一个文件似乎工作正常,直到它实际到达发出请求,此时,Fiddler中显示的网址与我尝试提出请求的内容不匹配,我不确定为什么.

正如您在下面看到的那样,请求应该是我的域/ api/product/GetToken api服务,但在Fiddler中,我将其显示为"/ user/[object%20Object]",而我不确定至于为什么会这样.此外,控制台不会产生任何错误.

以下是有问题的指令:

angular.module('myApp').directive('product', function($location) {
 return {
    restrict: 'E',
    replace: false,
    templateUrl: 'path/to/template.html',
    scope: {},
    controller: function($scope, $state, $cookieStore, $http) {
     $scope.productClick = function(key) {
      var url = 'http://exampleurl.com/api/product/GetToken';
      $http.get({url:url})
       .success(d, s, h, c) {
        $state.go('this.someplace');
      }
       .error(function(d, s, h, c) {
        $state.go('this.otherview');
       });
     },
     link: function($scope, e, a, m) {
      $scope.name = a.name + "123";
     }
    }
 }
}

有没有人知道我能做什么,我没有抓到这里?

1 个回答
  • 使用$ http和$ http.get有所不同.只需$ http,就像你写的那样(发送配置对象),但是使用$ http.get(或post等),你将url作为字符串传递.所以它是:

    $http.get('/my/url/').success(...).error(...);
    

    要么

    $http.post('/my/url/', dataObject).success(...).error(...);
    

    或只需$ http

    $http({url: '/my/url', method: 'GET'}).success(...).error(...);
    

    2023-01-08 08:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有