I am new to AngularJS, and building an app that will interact with a server. The server has a REST API, but responds to some methods with plain text, and to others with JSON. I have implemented a simple http request method using AngularJS' $resource
service.
However, when the server response is plain text, the response in AngularJS is an object with one entry for each character in the response word. How can I get around this (in a good way)? Ideally, I would like to be able to tell my service when to expect plain text and when to expect JSON, and get a nicely formatted response in both cases.
$resource is a convenience wrapper for working with Restful objects. It'll automatically try to parse as JSON and populate the object based on the $resource definition.
You are much better off using the $http service for non restful resources.
This is a lower level API that doesn't have such overbearing object mapping.
e.g.
$http({method: "GET", url: "/myTextDocURL"})
.success(function(data){
// data should be text string here (only if the server response is text/plain)
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With