【问题标题】:Why won't $http.put in AngularJS work?为什么 $http.put 在 AngularJS 中不起作用?
【发布时间】:2015-04-26 00:03:17
【问题描述】:

我目前在页面中有以下角度脚本:

var app = angular.module('doorman', []);
app.controller('formCtrl', function($scope, $http) {
  $scope.create = function() {
    var msg = '{' +
      '"id":"id",' +
      '"building":"' + $scope.building + '",' +
      '"unit":"' + $scope.unit + '",' +
      '"firstName":"' + $scope.firstName + '",' +
      '"name":"' + $scope.name + '",' +
      '"carrier":"' + $scope.carrier + '",' +
      '"tracker":"' + $scope.tracker + '"' +
      '"delivered":"0",' +
      '"added":"NOW()"' +
      '}';
    $http.put("104.131.166.246:8080/doorman/rest/pilot", msg).
    success(function(data, status, headers, config) {
      //empty the fields
      $scope.building = "";
      $scope.unit = "";
      $scope.firstName = "";
      $scope.name = "";
      $scope.carrier = "";
      $scope.tracker = "";
    }).
    error(function(data, status, headers, config) {
      //TODO temporary, remove
      alert("ERROR " + status + ": " + data);
    });
  };
  $scope.search = function() {

  }
});

我有一个后端 RESTful java servlet 处理请求。 当我调用“create()”函数时,输出总是以错误函数结束,显示警报“错误未定义:未定义”。在 chrome 上监控 http 请求时,我发现根本没有发出任何请求。可能是什么问题?

【问题讨论】:

  • 旁注:使用 $http 您的 msg aka 数据。应该是 javascript 对象文字而不是 JSON 字符串。

标签: javascript json angularjs http


【解决方案1】:

从角度来看,您的代码看起来不错。此外,当您说呼叫在错误部分结束时,它表明出站呼叫或服务器端出现问题。您可以使用浏览器工具吗,或者如果您在 Windows 中,请使用 fiddler 之类的工具来查看网络流量以了解发生了什么。一个

其他的,看看你是否必须在服务器端启用跨域资源共享(CORS)。

干杯

【讨论】:

  • 刚试了fiddler,结果和chrome一样:根本没有请求。这意味着问题肯定出在客户端。
【解决方案2】:

发现错误。显然,需要在 URL 的开头附加“http://”。还是谢谢!

【讨论】:

  • 绝对有必要。 Angular 不会假设协议。我正要发布一个答案,但你自己发现了错误。做得好。 :-)
猜你喜欢
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
相关资源
最近更新 更多