に私はJavaでのRESTful Webサービスに取り組んでいると私は有効なJSONを返すこのメソッドを持つJSONオブジェクトを送り返す:は、応答(ない文字列バージョン)
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getJSON()
{
String json = "{\"data\": \"This is my data\"}";
return Response.ok(json).build();
}
私が午前問題がありますJSONが文字列形式であることを示します。 オブジェクトフォームに戻すにはどうすればよいですか?応答データは、ここではJavaScript側から自分のWebサービス呼び出し
//Use of the promise to get the response
let promise = this.responseGet()
promise.then(
function(response) { //<--- the param response is a string and not an object :(
console.log("Success!", response);
}, function(error) {
console.error("Failed!", error);
}
);
//Response method
responseGet()
{
return new Promise(function(resolve, reject) {
let req = new XMLHttpRequest();
req.open('GET', 'http://localhost:8080/TestWebService/services/test');
req.onload = function() {
if (req.status == 200) {
resolve(req.response);
}
else {
reject(Error(req.statusText));
}
};
req.onerror = function() {
reject(Error("There was some error....."));
};
req.send();
});
}
おそらく[JavaでJSONとしてのHTTPResponse]と複製されています(http://stackoverflow.com/questions/17191412/httpresponse-as-json-in-java) –