简体   繁体   中英

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

"post ......... internal server error 500" help please this is code

<!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. 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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