2015-11-06 87 views
1

您好我正在用Angularjs創建一個應用程序,REST服務與春天。我想從angulajs網址休息服務傳遞對象,但它的工作,請任何一個可以幫助我,我的jsp頁面的代碼如下圖所示,將對象從angularjs控制器傳遞到休息API

<html ng-app="studentApp"> 
<body> 
<div ng-controller="studentController"> 
<table border = "0"> 
<tr> 
      <td>Enter first name:</td> 
      <td><input type = "text" ng-model = "student.firstName"></td> 
     </tr> 

     <tr> 
      <td>Enter last name: </td> 
      <td> 
       <input type = "text" ng-model = "student.lastName"> 
      </td> 
     </tr> 
</table> 
</div> 
</body> 
</html> 

和我angularjs代碼,

var studentApp = angular.module("studentApp", []); 

    studentApp.config(['$httpProvider', function ($httpProvider) { 
    $httpProvider.defaults.useXDomain = true; 
    delete $httpProvider.defaults.headers.common['X-Requested-With']; 
    }]); 

studentApp.controller("studentController", [ '$scope', '$http', 
function($scope, $http) { 

$scope.toggle=true; 
var urlBase="http://localhost:8080/studentweb/";   

    $scope.insertStudent = function inserStudent() { 

     $http({ 
      method : 'POST', 
      url : urlBase+'/Student/insert', 
      data: {student:data} 
     }).success(function(data, status, headers, config) { 
      $scope.student=data; 
      $scope.toggle='!toggle'; 
     }).error(function(data, status, headers, config) { 
      alert("failure1"); 
     }); 

和我的休息服務,

public class StudentController 
{ 
@RequestMapping(value="/Student/insert",method = RequestMethod.POST ,params= {"student"}) 
public String insertStudent(@RequestParam("student") StudentVO student) throws ParseException {  

    student.setFirstName(student.getFristName()); 
    student.setLastName(student.getLstName());  
    studentcontrol.addStudent(student);  

    return ""; 

} 

} 
} ]) 
+0

我猜$ .params是jQuery。我還沒有在文檔中找到任何這樣的功能。另外,在發佈的HTML中沒有ng-app。發佈一個重現問題的最小完整示例,並告訴*如何*它不起作用。打開瀏覽器控制檯並檢查錯誤消息。爲什麼你的REST API不接受JSON?這會讓事情變得更容易。 –

+0

警報消息failure1即將到來。控制檯上沒有錯誤日誌 – premashree

+0

您現在正在將請求正文中的JSON對象(作爲JSON)發送到不期待JSON的REST服務。 –

回答

1

的問題是,你「usrlBase」變量「學生/」額外的,你已經調用 網址你的學生控制器:urlBase +'/梭哈耳鼻喉科/插入」 因此,完整的URL變得像 http://localhost:8080/student//Student/insert,而應該是這樣的: http://localhost:8080/Student/insertStudent

更新: 下面是與你有一些括號丟失樣本RESTful服務的精絕工作示例你的代碼。請通過下面的代碼,如果需要回復我。

<html ng-app="studentApp"> 
<div ng-controller="studentController"> 
    <table border="0"> 
     <tr> 
      <td>Enter first name:</td> 
      <td><input type="text" ng-model="student.FirstName"></td> 
     </tr> 
     <tr> 
      <td>Enter last name: </td> 
      <td> 
       <input type="text" ng-model="student.LastName"> 
      </td> 
     </tr> 
    </table> 
</div> 

腳本: VAR studentApp = angular.module( 「studentApp」,[]);

studentApp.config(['$httpProvider', function ($httpProvider) { 
     $httpProvider.defaults.useXDomain = true; 
     delete $httpProvider.defaults.headers.common['X-Requested-With']; 
    }]); 

    studentApp.controller("studentController", ['$scope', '$http', 
    function ($scope, $http) { 

     $scope.toggle = true; 
     //var urlBase = "http://localhost:8080/student/"; 

     // $scope.insertStudent = function() { 
      debugger; 
      $http({ 
       method: 'GET', 
       url: 'http://services.odata.org/V4/Northwind/Northwind.svc/Employees(1)?$format=json', 
       // data: { student: data } 

      }).success(function (data, status, headers, config) { 
       debugger; 
       $scope.student = data; 
       $scope.toggle = '!toggle'; 
      }).error(function (data, status, headers, config) { 
       debugger; 
       alert("failure1"); 
      }); 
     // } 
    }]); 
+0

我更改了網址,但它完全不起作用 – premashree

相關問題