簡體   English   中英

如何從服務器向客戶端發送正文消息

[英]how to send body message from server to client

我有這個java服務器代碼:

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

和此客戶端代碼:

            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));

            });

如何在客戶端中訪問服務器響應中輸入的字符串?

我嘗試失敗:

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."}"

嘗試將.type(MediaType.TEXT_PLAIN)添加到您的響應中。

return Response.status(code).type(MediaType.TEXT_PLAIN).entity("{ \"errorMsg\": \""+error+"\"}").build();

您的響應不包含稱為errorMsg的鍵。 這就是其中包含的全部內容: 在此處輸入圖片說明

這個答案可能對您有用: JAX-RS-如何一起返回JSON和HTTP狀態代碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM