저는 angularjs를 처음 사용했기 때문에 데이터베이스에 세부 정보를 저장할 수 없었습니다. 브라우저에서 출력 결과를 성공적으로 저장하고 있지만 PostgreSQL에서는 데이터를 저장하지 않았습니다. 나는 이틀 동안 여기에서 강타했다. 누군가이 코드를 도울 수 있을까? 문제는 주로 Ajax 데이터베이스 호출과 관련이 있다고 생각합니다. 다음은 코드의 일부입니다. 나는 모든 가능성을 시도했지만 도달 할 수 없었습니다. 이를위한 해결책은 무엇입니까?Angularjs ajax 호출이 postgresql에 작동하지 않습니다.
> **Default.aspx**
<div data-ng-app="myApp" data-ng-controller="myCntrl">
<table>
<tr>
<td>
Student ID :
</td>
<td>
<input type="number" id="noStudentID" data-ng-model="studetnNo" />
</td>
</tr>
<tr>
<td>
Student Name :
</td>
<td>
<input type="text" id="txtStudentName" data-ng-model="studetnName" />
</td>
</tr>
<tr>
<td colspan="2" data-align="center">
<input type="button" value="Save" data-ng-click="Save()" />
</td>
</tr>
</table>
<script>
var app = angular.module("myApp",[])
app.controller("myCntrl",function($scope,$http)
{
$scope.studetnNo="";
$scope.studetnName="";
$scope.Save = function() {
var httpreq = {
method: 'POST',
url: 'Default.aspx/Save',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'dataType': 'json'
},
data: {
StudentID: $scope.studetnNo,
StudentName: $scope.studetnName
}
};
$http(httpreq).success(function (response) {
$scope.fillList();
alert("Saved Succefully");
});
};
});
</script>
> Default.aspx.cs
[System.Web.Services.WebMethod()]
public static void Save(int StudentID,string StudentName)
{
using (NpgsqlConnection con = new NpgsqlConnection(@"Server=10.0.5.22;Port=5432;Database=TEST_DB;User Id=postgres;Password=test;"))
{
using (NpgsqlCommand cmd = new NpgsqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "insert into student1(StudentID,StudentName)values(@StudentID,@StudentName);"; //DB table name and columns are correct
cmd.Parameters.AddWithValue("@StudentID", StudentID);
cmd.Parameters.AddWithValue("@StudentName", StudentName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
바꾸기; with, 줄 'Content-Type': 'application/json; – Vivz
","로 교체 한 후에도 작동하지 않습니다. –
어떤 각도 버전을 사용하고 있습니까? – Vivz