简体   繁体   English

如何将数据从Angular发布到Web API,以便发送具有相同数据的电子邮件(无数据库)

[英]How to POST data from Angular to Web API in order to send email with same data (no database)

The user adds items -> the model data is pushed to an empty object to create the order. 用户添加项目->将模型数据推送到空对象以创建订单。 Start with: 从...开始:

$scope.Order = {
    details:{},
    type1: [],
    type2: [],
    type3: [],
};

Then: 然后:

$scope.addType1 = function ()
        $scope.Order.type1.push({
            name: $scope.addType1.name,
            price: $scope.addType1.price
        });

Followed by the remaining types. 其次是剩余的类型。

I added a WebApi project (so now there are two projects), and I created the same Models in the API. 我添加了一个WebApi项目(因此现在有两个项目),并且在API中创建了相同的模型。

What I need to do: Post data from Angular to the WebAPI so that an email can be sent containing the order data. 我需要做的:将数据从Angular发布到WebAPI,以便可以发送包含订单数据的电子邮件。

I am familiar with the $Http methods in Angular, but I'm not sure how to check if the data has been transferred to the API, and I'm not sure what my API controller needs. 我熟悉Angular中的$ Http方法,但是不确定如何检查数据是否已传输到API,也不确定API控制器需要什么。 I don't have much experience with server-side code, so any help is appreciated. 我在服务器端代码方面经验不足,因此可以提供任何帮助。

Also, let me know if you need more information and I can explain further. 另外,如果您需要更多信息,请告诉我,我可以进一步解释。

 var app = angular.module('myApp', ['ui.bootstrap', 'angularUUID2']);

app.controller('myCtrl', ['$scope', '$http', '$q', 'uuid2', function      ($scope, $http, $q, uuid2) {

$scope.order = {
    details: {},
    type1: [],
    type2: []
    };

    $scope.addOrderDetails = function () {

        $scope.addOrderDetails.orderId = uuid2.newguid();

        $scope.order.details = $scope.addOrderDetails;

        };

    $scope.addType1 = function () {
        $scope.order.type1.push({
            xxx: $scope.addType1.xxx,
            yyy: $scope.addType1.yyy,
            zzz: $scope.addType1.zzz
        });
    };

    $scope.addType2 = function () {
                $scope.order.type2.push({
                    xxx: $scope.addType2.xxx,
                    yyy: $scope.addType2.yyy,
                    zzz: $scope.addType2.zzz
                });
            };

 var deferred = $q.defer();

    var url = 'http://localhost:xxxxxx/api/order';

    var data = $scope.order;


    $scope.submitOrder = function () {
        $http.post(url, data).success(function (response) {
            console.log(response);
            deferred.resolve(response)
        }).error(function (error) {
            console.log(error);
            deferred.reject(error)
        });
        return deferred.promise;   
    };
}]);

Here is the OrderController Post method: 这是OrderController Post方法:

[HttpPost]
    public Order Post([FromBody]Order model)
    {
        return model;
    }

There you have documentation for $http service, which allows to POST and GET. 那里有$ http服务的文档,该文档允许POST和GET。

https://docs.angularjs.org/api/ng/service/ $http https://docs.angularjs.org/api/ng/service/ $ http

So: 所以:

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

Basically, if the response is a success response (server responds positively), the callback gets executed. 基本上,如果响应是成功响应(服务器响应为肯定),则执行回调。 The same for failure. 失败也一样。

uksz is right! uksz是对的!

just wanted to show how you would post your data 只是想展示您将如何发布数据

var url = your url to post to here e.g 'www.somedomain.com/orders';

var data = put your data object here e.g. $scope.order;  

$http.post(url,data).success(function(response) {
                 console.log(response);
               }).error(function(error) {
                 console.log(error);
               });

You would usually want to implement a promise in this situation, which you can do like so (just make sure you include the $q library in your dependencies 您通常希望在这种情况下实现一个promise,您可以这样做(只需确保在依赖项中包括$ q库)

var deferred = $q.defer()

var url = your url to post to here e.g 'www.somedomain.com/orders';

var data = put your data object here e.g. $scope.order;  

$http.post(url,data).success(function(response) {
                 console.log(response);
                 deferred.resolve(response)
               }).error(function(error) {
                 console.log(error);
                 deferred.reject(error)
               });
               return deferred.promise

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

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