我有一个名为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"; } } } }
有没有人知道我能做什么,我没有抓到这里?
使用$ 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(...);