angularJS POST请求MVC4 WebAPI2不起作用

时间:2015-01-22 21:37:41

标签: angularjs asp.net-mvc-4 http-post

正如标题所说,我正在尝试做一个angularJS http POST。目标是将数据($ scope.econData对象)发布到我的本地WebAPI 2应用程序。我知道帖子正在执行,因为数据正在写入我的测试数据库(SQL Server,但我认为这是无关紧要的)。但是,所有正在写入的数据都是空的,因此未正确发布。我的html / js:

<html ng-app="" ng-controller="econAppPostController">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
    <!-- <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!--<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>></script>
    <meta name="viewport" content="width=device-width, initial-scale=1"> -->
</head>
<body>

    {{angularVerify}}
    {{$scope.responseEconData.item1}}

    <script>
        function econAppPostController($scope, $http) 
            {
            $scope.angularVerify = "angularJS is running...";
            $scope.econData = { "item1": 30, "item2": 40, "item3": 20, "item4": 20, "item5": 20, "item6": 20, "item7": 20, "item8": 20, "item9": 20};

            $http.post("http://localhost:XXXXXX/api/econdata", $scope.econData)
            .success(function (response) { $scope.responseEconData = response; });
            }
    </script>

</body>
</html>

我的控制器代码如下:

 public EconData POST(EconData x)
            {

            EconData econDataInstance = new EconData();
            econDataInstance.sqlWriteString = "INSERT INTO EconData(item1,item2, item3, item4,item5, item6, item7,item8, item9) VALUES (" + 
            econDataInstance.item1 + "," + 
            econDataInstance.item2 + "," + 
            econDataInstance.item3 + "," + 
            econDataInstance.item4 + "," + 
            econDataInstance.item5 + "," + 
            econDataInstance.item6 + "," + 
            econDataInstance.item7 + "," + 
            econDataInstance.item8 + "," + 
            econDataInstance.item9 + ")";


            econDataInstance.item1 = x.item1;
            econDataInstance.item2 = x.item2;
            econDataInstance.item1 = x.item3;
            econDataInstance.item2 = x.item4;
            econDataInstance.item1 = x.item5;
            econDataInstance.item2 = x.item6;
            econDataInstance.item1 = x.item7;
            econDataInstance.item2 = x.item8;
            econDataInstance.item2 = x.item9;

            //----------------OPEN CONNECTION------------------------------------------------
            SqlConnection myConnection = new SqlConnection("user id=username;" + 
                                           "password=password;" + 
                                           @"Data Source=XXX\SQLEXPRESS;" + 
                                           "Trusted_Connection=yes;" + 
                                           "Initial Catalog=econAppDB;" + 
                                           "connection timeout=30");
            myConnection.Open();
            //----------------OPEN CONNECTION------------------------------------------------

            //----------------WRITE DATA-----------------------------------------------------
            SqlCommand insertDataCommand = new SqlCommand (econDataInstance.sqlWriteString, myConnection);
            insertDataCommand.ExecuteNonQuery();
            //----------------WRITE DATA-----------------------------------------------------

            //----------------CLOSE CONNECTION------------------------------------------------
            myConnection.Close();
            //----------------CLOSE CONNECTION------------------------------------------------
            return econDataInstance;
            }
        }

------------------------只是尽职调查-------------

http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ - 我根本不理解这一点,只是没有帮助。

angularjs + spring mvc + json post request - 这对我不起作用

http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm - 或者

我已经在这里待了半天了,请告诉我解决问题的最简单方法。在这一点上,我并不关心它是否合适。对于post btw来说,这肯定是一个问题。最后,我已经阅读了官方的角度文档,并且没有完全理解如何格式化帖子,所以如果有人可以将我链接到某些东西,那将会有很大帮助。

0 个答案:

没有答案