【问题标题】:Angularjs $http post error in IonicIonic中的Angularjs $http发布错误
【发布时间】:2015-09-11 10:14:18
【问题描述】:

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

.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)

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

【问题讨论】:

  • 'Unexpected request' - 看起来你突然包含了 ngMock 模块(或其他用于测试的模块)
  • @STEVER 是的,完全正确。在app.js 中,我包含了ngMockE2E。这就是它在其他项目中工作的原因。我该怎么办?
  • @STEVER 我清理了ngMockE2E 并测试了部分,现在它可以完美运行了。如果您发布答案,我会接受它作为已接受的答案。谢谢。
  • 欢迎 :) 发布答案

标签: javascript angularjs http-post httpservice


【解决方案1】:

问题在于“ngMockE2E”AngularJS 模块,它应该只用于测试,而不是像项目的依赖项一样包含在内。

所以解决方案:只需删除此依赖即可。

【讨论】:

    【解决方案2】:

    你必须在你的 app.js 中描述 $httpbackend.whenPOST

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2016-09-18
      • 2015-11-01
      • 1970-01-01
      相关资源
      最近更新 更多