如何处理纯文本服务器的响应? [英] How to handle plain text server response?

查看:144
本文介绍了如何处理纯文本服务器的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来AngularJS,构建一个应用程序,将与服务器进行交互。该服务器有一个REST API,但响应的一些方法与纯文本,并给他人使用JSON。我已经实现用AngularJS $资源服务一个简单的HTTP请求方法。

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.

然而,当服务器响应是纯文本,在AngularJS响应是与在响应字的每个字符一个条目的对象。我怎样才能解决这个问题(一个好办法)?理想情况下,我希望能够告诉我的服务时,期望纯文本以及何时期待JSON,并获得在两种情况下一个很好的格式的响应。

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.

推荐答案

$资源是REST风格的对象时的便捷包装器。它会自动尝试基于$资源定义解析为JSON和填充的对象。

$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.

您是关闭使用 $ http服务获得更好非宁静的资源。

You are much better off using the $http service for non restful resources.

这是一个较低的水平API,它不具备这样的霸道对象的映射。

This is a lower level API that doesn't have such overbearing object mapping.

例如

$http({method: "GET", url: "/myTextDocURL"})
  .success(function(data){ 
      // data should be text string here (only if the server response is text/plain)
  }
);

这篇关于如何处理纯文本服务器的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆