Reputation: 79
I have jersey framework implemented for Rest services version 2.5
I have implemented Get, it works fine and response shows as JSON object in url when I have tried.
When I have tried the same url in angular JS using http.post and $ resource, success comes as 200k but there is no response. web app deployed in local on glassfish server and jersey rest services deployed in websphere 7
Rest controller
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
public Response assignAppointment(@PathParam("id") String id) {
Appointment app = new Appointment();
app.setId(id);
app.setTechName("fffff");
// return Response.status(200).entity(app).build();
return Response.ok(app).build();
}
in Angular JS
$http.get('http://mylocal.com/ntschedulerp/rest/appointment/'+$scope.appt.apptId,
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}}).then(function(appoinmentData)
{
$scope.assignmentForm = "Controller";
$scope.techName=appointmentData.data.techName;
$scope.response1=appointmentData.status;
});
response is empty but status code is 200. but when tried direct url it shows json object i browser. but while accessing from web app, the response is empty..checked in firebug
Upvotes: 1
Views: 1743
Reputation: 79
The issue got resolved. The issue occurred because of the response headers does not accept cross requests.
CORS http request.
Since I have rest server and web app in different servers, angular js does not directly accept the response which does not have specific headers.
To resolve this issue, Both server and client should have headers embedded.
I have resolved as follows
we need add these headers to the response
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods': ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' : 'Content-Type'
On client side, add the magic statement
$http.defaults.useXDomain = true;
before calling $http method in controller, I am using angular js 1.2 version, earlier versions, it might need to do like this..not tested but found some where
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
on server side, I am using jersey framework (2.5),
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
public Response assignAppointment(@PathParam("id") String id) {
Appointment app = new Appointment();
app.setId(id);
app.setTechName("xxxx");
ResponseBuilder response=Response.status(200).entity(app);
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "Cache-Control, Pragma,
Origin, Authorization, Content-Type, X-Requested-With");
response.header("Access-Control-Allow-Headers", "GET, PUT, OPTIONS,
X-XSRF-TOKEN");
return response.build();
}
one can use, @Context HttpServletResponse as method argument in rest methods, if using older versions.
Upvotes: 1