当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - Is there any other way except POJO to send response in form of JSON from server side to angular?

问题描述:

Hi i am at my elementary stage in learning dropwizard with angularjs, my problem is that when i send response from my resource at server side as POJO, it gets parsed into JSON, but if i send any String, angular throws me error, please help me out how can i send my custom response to angular.

网友答案:

First step is to set the response type to plain text. This could be done like this:

@Path("/example")
@Produces(MediaType.TEXT_PLAIN)
public class EndPoint{

    @GET
    public Response saySomething() {
        return Response.ok("plain text response").type(MediaType.TEXT_PLAIN).build();
    }

}

once you have that covered you can use angular's $http service to read the plain text response:

$http({method: "GET", url: "/example"})
  .success(function(data){ 
      alert(data) // alert: plain text response
  }
);
分享给朋友:
您可能感兴趣的文章:
随机阅读: