2016-09-17 82 views
1

我一直试图让HTTP POST在AngularJS上工作好几天。我现在真的很坚持,我真的很感激任何见解!对于服务器端我使用JAX-RS和Jersey。我传递的对象是application/json,我创建了一个Java类Student(请参阅下面的代码以获取更多详细信息)。我的目标是发布一个来自AngularJS的Student对象到服务器。Java REST HTTP POST在curl中工作,但不在AngularJS中

为了验证我的逻辑是从服务器端完成的,我使用了来自Google Apps的REST API客户端以及Unix命令curl。这两种方法都返回HTTP状态200.

然而,当我从AngularJS尝试同样的事情时,HTTP POST有错误,Response对象没有数据,状态为-1。该post请求似乎已到达服务器,因为它输出HTTP状态204. 我不明白的是为什么与AngularJS我得到的状态代码为204,但与curl/REST API客户端我得到200的状态,它的工作原理正常。

下面是我的代码的相关部分:

控制器定义,并包含HTTP POST调用一个函数addAName()

app.controller('PostingController', function($scope, $http) { 
$scope.postMessage='This message is a default message and will change after I click the button and addAName() is called.'; 
$scope.name='CODER'; 
$scope.postData = 'Not Available'; 

$scope.addAName = function(){ 
    var data = {"studentId": 2, "firstName": "Jason", "lastName": "Lee", "specialization": "Student"}; 

    $http.post("http:/localhost:8080/MathAssignment/studentservice/users", data).then(function successCallback(response) { 
     $scope.postMessage = "POSTING WORKS!"; 
     $scope.postStatus = response.status; 
     $scope.postData = response.data; 
    }, function errorCallback(response) { 
     $scope.postMessage = "POSTING STILL DOES NOT WORK"; 
     $scope.postStatus = response.status; 
     $scope.postData = response.data; 
    }); 
}; 
}); 

接受的HTTP POST调用服务器端代码:

@POST 
    @Path("/users") 
    @Produces(MediaType.APPLICATION_JSON) 
    @Consumes(MediaType.APPLICATION_JSON) 
    public Response updateUser(Student inputJsonObj) throws IOException { 
     int id = (int) inputJsonObj.getStudentId(); 
     String name = (String) inputJsonObj.getFirstName() ; 
     String profession = (String) inputJsonObj.getSpecialization(); 
     System.err.println("JSON is" + inputJsonObj.toString()); 
     System.err.println("Name is" + name); 
     return Response.status(200).entity(inputJsonObj).build(); 
    } 

Student class definition:

public class Student { 

    private int studentId; 
    private String firstName; 
    private String lastName; 
    private String specialization; 

    public int getStudentId() { 
     return studentId; 
    } 
    public void setStudentId(int studentId) { 
     this.studentId = studentId; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
    public String getSpecialization() { 
     return specialization; 
    } 
    public void setSpecialization(String specialization) { 
     this.specialization = specialization; 
    } 
} 
+1

您也可以尝试寻找原始数据包 - 一个工程和一个不 - ,看看您是否可以手动看出其中的区别。网络嗅探器或TCPDump将在这里帮助。 – markspace

+1

感谢您的快速回复。我没有创建WSDL文件,因为这是一个REST调用。我认为他们只需要SOAP/XML风格的服务。这是不正确的? –

+0

不,他们不是。我的客户端在localhost:63342上运行,因为我使用的是IDE Webstorm。我的服务器在localhost:8080上运行。 –

回答

1

REST服务方法的声明包括:

@Consumes(MediaType.APPLICATION_JSON) 

这表明,该方法消耗了内容类型 “application/JSON” 的。

为了达到此方法,您需要添加一个Content-type标头,其值为“application/json”。 喜欢的东西:

$http.post("/foo/bar", requestData, { 
     headers: { 'Content-type': 'application/json'} 
    }).success(function(responseData) { 
     //do stuff with response 
    }); 
+0

您好,我尝试了您的建议并添加了一个Content-type标头。不幸的是,我仍然得到相同的结果。 –

相关问题