httpj以angularjs形式发布

时间:2017-04-12 04:55:51

标签: angularjs forms http post

我正在尝试从AngularJS中的表单获取参数,并将其发送到以这样的URL编码的远程Web API。 http://lowc---.com/storeManager/createParentStore?token=6fc72be8-4153-432e-9191-64a9e3b23a9e&name=xyz%20store&city=1&address=abc

但我收到以下错误

 - TypeError: $http(...).success is not a function

 - Failed to load resource: the server responded with a status of 404 (Not Found)

 - Possibly unhandled rejection: {"data":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /storeManager/createParentStore</pre>\n</body>\n</html>\n","status":404,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://low----.com/storeManager/createParentStore","data":{"name":"$scope.user.name","city":"$scope.user.city","address":"$scope.user.address"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"Not Found"}angular.js:14328 Possibly unhandled rejection: {"data":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /storeManager/createParentStore</pre>\n</body>\n</html>\n","status":404,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://lowcost-env.2u3kmcbg4k.us-west-2.elasticbeanstalk.com/storeManager/createParentStore","params":{"name":"$scope.user.name","city":"$scope.user.city","address":"$scope.user.address"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"Not Found"}`

<!DOCTYPE html>
<html lang="en" ng-app="mainApp">

<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>

  <script src="addRetailerCtrl.js"></script>
</head>

<body ng-controller="addRetailerCtrl">

  <form class="form-horizontal" name="userForm" ng-submit="submitForm()">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><strong>Add new </strong></h3>

      </div>

      <div class="panel-body form-group-separated">

        <div class="form-group">
          <label class="col-md-3 col-xs-12 control-label">Name</label>
          <div class="col-md-6 col-xs-12">
            <div class="input-group">
              <span class="input-group-addon"><span class="fa fa-pencil"></span></span>
              <input type="text" class="form-control" name="name" ng-model="user.name" />
            </div>

          </div>
        </div>

        <div class="form-group">
          <label class="col-md-3 col-xs-12 control-label">City</label>
          <div class="col-md-6 col-xs-12">
            <select class="form-control select" name="city" ng-model="user.city">
                                                <option>City 1</option>
                                                <option>City 2</option>
                                                <option>City 3</option>
                                                
                                            </select>

          </div>

        </div>
        <div class="form-group">
          <label class="col-md-3 col-xs-12 control-label">Address</label>
          <div class="col-md-6 col-xs-12">
            <textarea class="form-control" rows="5" name="address" ng-model="user.address"></textarea>

          </div>

        </div>

angular.module('mainApp', [])
  .controller('addRetailerCtrl', function($scope, $http) {
    alert('i am here');

    $scope.submitForm = function() {
      // Posting data to php file
      $http({
          method: 'POST',
          url: 'http://low--.com/storeManager/createParentStore?token=6fc72be8-4153-432e-9191-64a9e3b23a9e',
          // data    : $scope.user, //forms user object
          data: {
            name: "$scope.user.name",
            city: "$scope.user.city",
            address: "$scope.user.address"
          },
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
        .success(function(data) {

          $scope.message = data.message;

        });
    };
  });

1 个答案:

答案 0 :(得分:1)

{strong> success() error() 已被 $http's 自定义回调方法弃用{{{ 3}}。
您可以使用标准的 then()/catch() promise方法,但请注意方法签名和返回值不同。