【问题标题】:calling function containing `$http.get()` on button press按下按钮时调用包含`$http.get()`的函数
【发布时间】:2013-11-06 01:09:18
【问题描述】:

调用包含$http.get()的函数

$scope.storeDb = function() {

    $scope.url = '/some';
    $http({method: 'GET', url: $scope.url}).
    success(function(data, status, headers, config) {

        console.log(data);
        alert(status);

    }).
    error(function(data, status, headers, config) {

      alert('error is ' + status);

    });
  }

我正在从ng-click调用函数

<input type="submit", value='ask', ng-click='storeDb()'/> 

警报消息是error is 0

【问题讨论】:

  • 查看您的浏览器控制台日志和网络选项卡,了解错误或不正确的 url。
  • 使用浏览器控制台检查请求...需要比给出的更多细节
  • 正在处理它。让我回去。
  • @Chandermani 做了一些诊断。仍然无法得到它。有错误但状态为0
  • @charlietfl 做了一些诊断。仍然无法得到它。有错误但状态为 0

标签: http angularjs angularjs-service


【解决方案1】:

尽管我相信 jkschneider 是正确的,但您似乎确实有错误,调试它应该可以帮助您找到问题,请指出 2 个错误:)

第一个是$http.get的误用,它应该是这样的

$http({
    method: 'GET', url: '/someUrl'}).
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });

也可以像 $http.get().success().error();

看这里http://docs.angularjs.org/api/ng.$http

其次,您应该使用 MVVM/MVC 方式来获取/更新应用程序中的数据(即您需要有一个模型),否则您可能会遇到一些其他问题。我用完整的代码示例写了一个简短的教程http://bresleveloper.blogspot.co.il/2013/08/breslevelopers-angularjs-tutorial.html

【讨论】:

    【解决方案2】:

    then 处理程序添加另一个函数来处理错误情况,并调试或记录输出。

    【讨论】:

    • 做了一些诊断。仍然无法得到它。有错误但状态为 0
    猜你喜欢
    • 2013-02-11
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2019-08-26
    相关资源
    最近更新 更多