2015-09-11 75 views
0

这是我的控制器。当我想将一些数据发布到后端时,出现错误。

.controller('LoginCtrl', function($http, $scope, $state, $ionicPopup, AuthService) { 
    $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 
    $http({ 
     method: 'POST', 
     url: 'http://cms.focusweb.ir/Json/get_article', 
     data: { id: 25 }, 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
    }) 
    .success(function(response) { 
     // handle success things 
     console.log(response); 
    }) 
    .error(function(data, status, headers, config) { 
     // handle error things 
    }) 
}) 

错误我得到:

Error: Unexpected request: POST http://cms.focusweb.ir/Json/get_article 
No more request expected 
at $httpBackend (angular-mocks.js:1207) 
at sendReq (ionic.bundle.js:19160) 
at status.$get.serverRequest (ionic.bundle.js:18872) 
at processQueue (ionic.bundle.js:23394) 
at ionic.bundle.js:23410 
at Scope.parent.$get.Scope.$eval (ionic.bundle.js:24673) 
at Scope.parent.$get.Scope.$digest (ionic.bundle.js:24484) 
at Scope.parent.$get.Scope.$apply (ionic.bundle.js:24778) 
at done (ionic.bundle.js:19191) 
at completeRequest (ionic.bundle.js:19363) 

注:我已经使用在其他项目中该控制器的代码,但它工作正常。

+1

“意外的请求” - 它看起来像你突然包含ngMock模块(或用于测试其他模块) –

+0

@STEVER是的,没错。在'app.js'中我包含了'ngMockE2E'。这就是它在其他项目中的原因。我该怎么办? –

+0

@STEVER我清理了'ngMockE2E'和测试部分,现在它工作得很完美。如果你发布答案,我会接受它作为接受的答案。谢谢。 –

回答

2

问题出在“ngMockE2E”AngularJS模块应该只用于测试,不包括像你的项目依赖项。

所以解决方法:只是删除这个依赖。

-1

你要描述$ httpbackend.whenPOST在app.js

$httpBackend.whenPOST('http://cms.focusweb.ir/Json/get_article')`enter code here` 
    .respond();