0

i have this java server code:

private Response createError(int code, String error) {
    logger.error(error);
    return Response.status(code).entity("{ \"errorMsg\": \""+error+"\"}").build();
}

and this client code:

            function successCallback(response) {
                self.lastResults = response.data;
                self.filesToUpload.length = 0;
                deferred.resolve(response.data);
            }, function errorCallback(response) {
                console.log(response.data.errorMsg);
                self.filesToUpload.length = 0;
                deferred.reject(JSON.stringify(response.data.errorMsg));

            });

how can i access in the client the string I put in the response at the server?

I tried unsuccessfully:

response.getEntity()
VM828:1 Uncaught TypeError: response.getEntity is not a function
    at eval (eval at errorCallback (upload-service.js:114), <anonymous>:1:10)
    at errorCallback (upload-service.js:114)
    at processQueue (angular.js:16648)
    at angular.js:16692
    at Scope.$eval (angular.js:17972)
    at Scope.$digest (angular.js:17786)
    at Scope.$apply (angular.js:18080)
    at done (angular.js:12210)
    at completeRequest (angular.js:12436)
    at XMLHttpRequest.requestLoaded (angular.js:12364)
(anonymous) @ VM828:1
errorCallback @ upload-service.js:114
processQueue @ angular.js:16648
(anonymous) @ angular.js:16692
$eval @ angular.js:17972
$digest @ angular.js:17786
$apply @ angular.js:18080
done @ angular.js:12210
completeRequest @ angular.js:12436
requestLoaded @ angular.js:12364

response.entity
undefined

JSON.stringify(response)
"{"data":"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n<title>Error 500 Request failed.</title>\n</head>\n<body><h2>HTTP ERROR 500</h2>\n<p>Problem accessing /api/VoicesUploader/uploadFiles. Reason:\n<pre>    Request failed.</pre></p><hr><a href=\"http://eclipse.org/jetty\">Powered by Jetty:// 9.3.15.v20161220</a><hr/>\n\n</body>\n</html>\n","status":500,"config":{"method":"POST","transformResponse":[null],"jsonpCallbackParam":"callback","headers":{"Accept":"application/json, text/plain, */*"},"url":"/api/VoicesUploader/uploadFiles","data":{}},"statusText":"Request failed."}"
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

2 Answers2

0

Try to add .type(MediaType.TEXT_PLAIN) to your response.

return Response.status(code).type(MediaType.TEXT_PLAIN).entity("{ \"errorMsg\": \""+error+"\"}").build();
Olezt
  • 1,638
  • 1
  • 15
  • 31
0

Your response doesn't include a key called errorMsg. This is all that's being included: enter image description here

This answer might be useful to you: JAX-RS — How to return JSON and HTTP status code together?

JonLuca
  • 850
  • 6
  • 25