简体   繁体   English

AngularJS如何返回字符串而不是Json对象

[英]AngularJS how return a string instead of Json object

I use angularjs to get a string from a web service. 我使用angularjs从Web服务获取字符串。 This string is actually a base64 pdf file. 该字符串实际上是base64 pdf文件。

public string Get(int id)
{
    var bytes = File.ReadAllBytes(@"C:\test.pdf");
    var content = Convert.ToBase64String(bytes);
    return content;
}

Angularjs return me a array like this one: Angularjs返回了一个像这样的数组:

{"0":"\\"","1":"J","2":"V","3":"B","4":"E" ...} {“ 0”:“ \\”“,” 1“:” J“,” 2“:” V“,” 3“:” B“,” 4“:” E“ ...}

But I need a complete string like this : "JVBE..." 但我需要这样的完整字符串:“ JVBE ...”

I try to find a way to get the complete string instead of array format and I don't find anything. 我试图找到一种获取完整字符串而不是数组格式的方法,但是我什么也没找到。

Do you know how handle this? 你知道如何处理吗?

Thank you! 谢谢!

Karine 卡琳

It seems that you are returning data from .Net MVC's Web API. 看来您是从.Net MVC的Web API返回数据。 You can do 你可以做

public object Get(int id)
{
    var bytes = File.ReadAllBytes(@"C:\test.pdf");
    var content = Convert.ToBase64String(bytes);
    return new {value = content};
}

Since a string can't be converted to JSON. 由于无法将字符串转换为JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM