简体   繁体   English

从 Restful API 返回要显示在视图上的字符串

[英]Return a string to be displayed on a View from Restful API

I have an image of a barcode that I submit to a form.我有一张提交给表单的条形码图像。 The image is posted to a restful API.图像被发布到一个宁静的 API。 The API receives the image, processes the image and throws an exception or returns a string of the barcode from the image. API 接收图像,处理图像并抛出异常或从图像中返回条形码字符串。

Below is a snippet of my code下面是我的代码片段

View -看法 -

<form name="somename" method="post" enctype="multipart/form-data"
    action="/api/decoder">
    <input type="file" name="ImageToDecode" accept="image/*" capture="camera" />
    <input type="submit" value="Get Barcode..." />
< /form>

Api - api -

public class DecodeImageController : ApiController
{
    public string Post()
    {
        var postedImage = HttpContext.Current.Request; // get the posted image

        // do some stuff in here to read the barcode from the image
        if (!could read image)
        {
            return barcode.ToString();
        }

        // barcode could not be found
        throw new Exception("Barcode could not be read");   
    }   // end post method
}

I am using Visual Studio 2013 MVC project.我正在使用 Visual Studio 2013 MVC 项目。 The API and the View are both in separate projects. API 和视图都在不同的项目中。 This is what gets returned.这就是返回的内容。

This XML file does not appear to have any style information associated with it.此 XML 文件似乎没有任何与之关联的样式信息。 The document tree is shown below.文档树如下所示。

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">10320972351</string>

This works fine, but now I want to call the API and have the returned barcode to my Controller.这工作正常,但现在我想调用 API 并将返回的条形码发送到我的控制器。

Any direction would be helpful.任何方向都会有所帮助。

I am continuing to work on this as ideas pop into my head.随着想法涌入我的脑海,我将继续致力于此。

This response is correct. 这个回答是正确的。 You synchronously post form and receive correct answer. 您同步发布表单并收到正确答案。

The solution - use Json to exchange data and use jquery like $.post to send asynchronous query and perform required actions in success method of $.post 解决方案-使用Json交换数据并使用$ .post之类的jquery发送异步查询并以$ .post的成功方法执行所需的操作

In addition to @Vladmir's answer... 除了@Vladmir的答案...

You can configure your API to return string in json data type, however you will need to add formatter to return json response from API. 您可以配置API以json数据类型返回字符串,但是您将需要添加格式化程序以从API返回json响应。 Refer this post How do I get ASP.NET Web API to return JSON instead of XML using Chrome? 请参阅这篇文章如何使用ASP如何获取ASP.NET Web API以返回JSON而不是XML? to know how to add formatter. 知道如何添加格式化程序。

After that you can use malsup jquery plugin to post your image to API via ajax with content type "text/html" would result barcode value in json. 之后,您可以使用malsup jquery插件通过内容类型为“ text / html”的ajax将图像发布到API,从而在json中生成条形码值。

This is what I found thanks to @Vladmir and @Mihir. 感谢@Vladmir和@Mihir,这就是我发现的东西。

First I used @Mihirs answer by adding a JsonFormatter to the WebApiConfig. 首先,我通过向WebApiConfig添加JsonFormatter来使用@Mihirs答案。 This seemed to be sufficient to return the string not in xml. 这似乎足以返回不在xml中的字符串。

and used some client side code provided here- 并使用了此处提供的一些客户端代码-

http://www.codeproject.com/Articles/806075/File-Upload-using-jQuery-AJAX-in-ASP-NET-Web-API http://www.codeproject.com/Articles/806075/File-Upload-using-jQuery-AJAX-in-ASP-NET-Web-API

I also got a Cross origin error which I resolved thants to this article: enabling cross-origin resource sharing on IIS7 我还遇到了一个跨源错误,该错误已解决,本文: 在IIS7上启用跨域资源共享

Thanks for pointing me in the right direction :) 感谢您指出正确的方向:)

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

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