简体   繁体   English

使用数据发送angularjs中的ajax请求

[英]Send ajax request in angularjs with data

I have form like below. 我的表格如下。 Here I have taken value of each element and send it to server via ajax call. 在这里,我获取了每个元素的值,并通过ajax调用将其发送到服务器。 Is their any easy way to send request to server using all values in form ?? 他们是否有任何简单的方法使用表单中的所有值向服务器发送请求? I am a newbie please help. 我是新手,请帮忙。 My form has a lot of element its very hard to take value of all element, is their any alternative method? 我的表单有很多元素很难取得所有元素的价值,是他们的替代方法吗?

<div ng-app="Myapp">
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.js"></script>
    <script>
        var Myapp = angular.module('Myapp', ["ngRoute"]);
    </script>

    <div ng-controller="orderFormController">
        Item1<input type="text" name="item1"  ng-model='item1'><p></p>
        Item2 <input type="text" name="item2"  ng-model='item2'><p></p>
        Item3 <input type="text" name="item3"  ng-model='item3'><p></p>
         Item4 <input type="text" name="item4"  ng-model='item4'><p></p>
        Item5 <input type="text" name="item5"  ng-model='item5'><p></p>
        <button type="button"  ng-click='saveAll()' >SAVE ORDER</button>
    </div>    
    <script>
        Myapp.controller('orderFormController', ['$scope', '$http', function ($scope, $http) {

                 var data = {};
                $scope.saveAll = function () {
                    data = {'item1': $scope.item1,'item2': $scope.item2,'item3': $scope.item3,'item4': $scope.item4}
                    $http.post("order/save/", data
                        ).success(function (res, status, headers, config) {
                    if (res == 'success')
                    {
                        $scope.message = res;
                    }
                    else
                    {
                        $scope.message = res;
                    }
                }).error(function (res, status) { 
                    $scope.message = res;
                });
                }

            }]);

    </script>               

Place a parent object on the scope and wire your properties to it. 在范围上放置父对象并将属性连接到它。 The parent object is then what you send. 然后父对象就是你发送的内容。

<div ng-app="Myapp">
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.js"></script>
    <script>
        var Myapp = angular.module('Myapp', ["ngRoute"]);
    </script>

    <div ng-controller="orderFormController">
        Item1<input type="text" name="item1"  ng-model='data.item1'><p></p>
        Item2 <input type="text" name="item2"  ng-model='data.item2'><p></p>
        Item3 <input type="text" name="item3"  ng-model='data.item3'><p></p>
         Item4 <input type="text" name="item4"  ng-model='data.item4'><p></p>
        Item5 <input type="text" name="item5"  ng-model='data.item5'><p></p>
        <button type="button"  ng-click='saveAll()' >SAVE ORDER</button>
    </div>    
    <script>
        Myapp.controller('orderFormController', ['$scope', '$http', function ($scope, $http) {

                 var data = {};
                $scope.data = data;
                $scope.saveAll = function () {
                    $http.post("order/save/", data
                        ).success(function (res, status, headers, config) {
                    if (res == 'success')
                    {
                        $scope.message = res;
                    }
                    else
                    {
                        $scope.message = res;
                    }
                }).error(function (res, status) { 
                    $scope.message = res;
                });
                }

The right way is to just send your model to the server, using $resource (for REST) 正确的方法是使用$resource (用于REST)将模型发送到服务器

<div ng-controller="orderFormController">
    Item1
    <input type="text" name="item1" ng-model='item.item1'><p></p>
    Item2 
    <input type="text" name="item2" ng-model='item.item2'><p></p>
    Item3 
    <input type="text" name="item3" ng-model='item.item3'><p></p>
    Item4 
    <input type="text" name="item4" ng-model='item.item4'><p></p>
    Item5 
    <input type="text" name="item5" ng-model='item.item5'><p></p>

    <button type="button"  ng-click='saveAll()' >SAVE ORDER</button>
</div>  

$scope.item.$save(function(data) {

});

$item is a angularjs resource $item是一个angularjs资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM