如何处理纯文本服务器的响应?

编程入门 行业动态 更新时间:2024-10-12 14:21:22
本文介绍了如何处理纯文本服务器的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来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) } );

更多推荐

如何处理纯文本服务器的响应?

本文发布于:2023-10-12 04:38:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   文本   服务器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!