使用asp.net的angularjs在sql server数据库中保存数据

时间:2014-08-14 11:54:10

标签: asp.net sql-server angularjs

我有这个程序,我想用ASP.net前端的angularJS在SQl数据库中保存数据,但是当我点击提交按钮时我没有得到任何响应

我创建了一个Web服务文件 UserService.asmx

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class UserService : System.Web.Services.WebService
{
string con;
SqlConnection con1;
public UserService()
{
    con = ConfigurationManager.ConnectionStrings["tbt_db"].ConnectionString;
    con1 = new SqlConnection(con);
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string saveUserDetails(String ID, String firstName)
{
    SqlCommand cmd;
    try
    {
        con1.Open();
        cmd = con1.CreateCommand();
        cmd.CommandText = "INSERT INTO emp(ID,FirstName)VALUES(@ID,@FirstName)";
        cmd.Parameters.AddWithValue("@ID", ID);
        cmd.Parameters.AddWithValue("@FirstName", firstName);
        cmd.ExecuteNonQuery();
        return "Record Inserted Successfully";
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (con1.State == ConnectionState.Open)
        {
            con1.Close();
        }

    }
}

public string HelloWorld()
{
    return "Hello World";
}

}

我的脚本文件是

$scope.save = function () {
$http.Post({
    type: "POST",
    url: "UserService.asmx/saveUserDetails",
    data: "{'" + $scope.ID + "','" + $scope.firstName + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
    alert(msg.d);
},
error: function (msg) {
    alert(msg.d);
}
 });
 };

我的aspx文件是

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="memb.aspx.cs" Inherits="memb" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="script.js"></script>
<script src="js/angular.js"></script>
</head>
<body>
<form id="form1" ng-controller="UserCntrl" ng-submit="save()">
<div>
    <p>EMP ID: </p>
    <input id="Text1" type="text" placeholder="EMP ID" ng-model="ID" />
     <p>First Name: </p>
    <input id="Text2" type="email" placeholder="First Name" ng-model="firstName" />
    <br /><br /><br />
    <input id="Button1" type="submit" value="submit" />
  </div>
 <div>
    <input type="button" id="btnFetch" value="Fetch" ng-click="getUser()"/>
 </div>
 <div id="showdata">
 <table>
    <tr>
        <td>ID</td>
        <td>firstName</td>

    </tr>
    <tr>
        <td>{{ID}}</td>
        <td>{{firstName}}</td>

    </tr>
 </table>            
</div>
</form>
</body>

数据库中没有数据存储

1 个答案:

答案 0 :(得分:0)

$ http.post的正确语法是$ http.post(url,data,[config]); 请参阅:https://docs.angularjs.org/api/ng/service/ $ http#post

在你的情况下,你应该这样做

$scope.save = function () {

var url = "UserService.asmx/saveUserDetails";
var data = "{'" + $scope.ID + "','" + $scope.firstName + "'}";
var config = { headers: { "Content-Type": "application/json" } };

$http.Post(url, data, config).success(function (data, status, headers, config) {
                    console.log(data);
             })
            .error(function (data, status, headers, config) {
                    console.log(status);
            });
};