简体   繁体   English

ASP.NET AMX Web服务

[英]Asp.net asmx web services

I want to make web service that takes form data by angular Javascript $http.post and insert in into employee table but I got this error on the inspector 我想制作通过角度Javascript $ http.post接受表单数据的Web服务,并将其插入到employee表中,但是在检查器上出现此错误

"post ......... internal server error 500" help please this is code “发布.........内部服务器错误500”帮助,这是代码

<!doctype html>
<html ng-app="app">
<head>
    <title>testgrid</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-animate.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
    <link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
   <div ng-app="app" ng-controller="myctrl">
       <form>
        <input name="firstname"/>
           <input name="firstname" type="text" ng-model="firstname" /><br/>
           <input name="lastname" type="text" ng-model="lastname" /><br/>
           <input name="middlename" type="text" ng-model="middlename"/><br />
           <input name="addrss" type="text" ng-model="address" /><br />
           <input name="email" type="text" ng-model="email" /><br />
           <input name="salary" type="text" ng-model="salary" /><br />
           <input type="button" value="submit" ng-click="insertdata()"/><br />
        </form>
   </div>
    <script>
        var app = angular.module('app', [])
        var config = { headers: { "Content-Type": "application/json" } };
        app.controller('myctrl', function ($scope, $http) {
            $scope.insertdata = function () {
                $http.post("insertemployee.asmx/setemployeeData", { 'firstname': $scope.firstname, 'lastname': $scope.lastname, 'middlename': $scope.middlename, 'address': $scope.address, 'email': $scope.email, 'salary': $scope.salary })
                .success(function (data, status, headers, config) { console.log("Data inserted successfully");})
            }

        })
   </script>
</body>
</html>


       [WebMethod]
       public void getEmployeeData()
        {

            List <Employee>employeelist= new List<Employee>();
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using(SqlConnection con=new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Employee",con);
                con.Open();
                SqlDataReader rdr=cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Employee employee = new Employee();
                    employee.FirstName = rdr["FirstName"].ToString();
                    employee.LastName = rdr["LastName"].ToString();
                    employee.Salary = Convert.ToInt32(rdr["salary"]);
                    employeelist.Add(employee);
                }
                }
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(employeelist));
            }

    }
}

ASMX is for SOAP services. ASMX用于SOAP服务。 They are a legacy technology and you should really avoid using it. 它们是一项旧技术,您应该避免使用它。 If you're using the MS stack, Web API works perfectly with Angular (and any other JS framework). 如果您使用的是MS堆栈,则Web API可以与Angular(以及任何其他JS框架)完美配合。 You can get WCF to also work with it but it requires much more configuration to do the same thing a Web API project can provide with almost no configuration. 您可以使WCF也可以使用它,但是它需要更多的配置来完成Web API项目几乎不需要配置就可以提供的相同功能。

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

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