2017-11-23 3 views
0

저는 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(); 
       } 
      } 
     } 
+0

바꾸기; with, 줄 'Content-Type': 'application/json; – Vivz

+1

","로 교체 한 후에도 작동하지 않습니다. –

+0

어떤 각도 버전을 사용하고 있습니까? – Vivz

답변

0

각도 스크립트에서 구문이 잘못되었습니다 ('없음'). 시도해보십시오.

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"); 
     }); 
    } 
}; 
+1

을 사용하십시오. 또한 작동하지 않습니다. –

+0

먼저 [postman] (https://www.getpostman.com/)과 같은 프로그램으로 http 엔드 포인트에 전화를 걸 수 있습니까? 그냥 당신이 호출하는 http : // host : port/Default.aspx/Save가 올바른지 확인하십시오. – Marcus

관련 문제