Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle plain text server response?

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.

like image 334
Per Quested Aronsson Avatar asked Jul 26 '13 17:07

Per Quested Aronsson


1 Answers

$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)
  }
);
like image 186
Ian Haggerty Avatar answered Oct 24 '22 00:10

Ian Haggerty