2017-07-08 5 views
0

Ich versuche, Daten in db mit Feder, angularjs und Hibernet einzufügen. in app.js Datei Controller-Methode $http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City) hat nicht funktioniert Ich weiß nicht, was ist der Fehler, den ich getan habe.

AngularDate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

<html ng-app="taskManagerApp"> 

    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
     <title>AngularJS Task Manager</title> 
     <script data-require="[email protected]*" data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js"></script> 
     <script src="<c:url value=" /resources/js/app.js "/>"></script> 
    </head> 

    <body> 
     <div ng-controller="taskManagerController"> 
      <span>Add Task</span> 
      <div> 
       <div> 
        <table> 
         <tr> 
          <td> Name:</td> 
          <td> 
           <input type="text" ng-model="Name" /> 
          </td> 
         </tr> 
         <tr> 
          <td>City:</td> 
          <td> 
           <input type="text" ng-model="City" /> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           <button ng-click="addTask()" class="btn-panel-big">Add New Task</button> 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
     </div> 
    </body> 

</html> 

app.js

var taskManagerModule = angular.module('taskManagerApp', []); 
var t = angular.module('taskManagerController', []); 

taskManagerModule.controller('taskManagerController', function ($scope, $http) { 
    var urlBase = "http://localhost:8081/logistics"; 
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 

    //get all tasks and display initially 
    //add a new task 
    $scope.addTask = function addTask() { 
     if ($scope.Name == "" || $scope.City == "") { 
      alert("Insufficient Data! Please provide values for name and city"); 
     } 
     else { 
      alert("else"); 
      $http.post(urlBase + '/angular/insert/' + $scope.Name + '/' + $scope.City).success(function (data) { 
       alert("Task added"); 
       // $scope.tasks = data; 
       $scope.Name = ""; 
       $scope.City = ""; 
       //$scope.toggle='!toggle';  
      }); 
     } 
    }; 

    // Archive Completed Tasks 
}); 

//Angularjs Directive for confirm dialog box 

Kann mir jemand helfen, mein Problem zu beheben?

Antwort

1

Der $http Post erwartet, dass der POST Körper so, wenn Sie es haben dann diese

$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City) 

mit

//JSON data to post 
var postData = { 
    ... 
    ... 
} 
$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City, postData).success(
function(data) { 
    //success code 
}) 

ersetzen oder wenn Sie keine Post-Daten haben Sie dann einfach ein leeres JSON-Objekt senden wie

$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City,{}).success(
    function(data) { 
     //success code 
    })