单值的WebAPI的方法和AngularJs $ HTTP JSON处理 [英] WebApi Method with single value and AngularJs $http json handling

查看:151
本文介绍了单值的WebAPI的方法和AngularJs $ HTTP JSON处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我跑出去的想法,我需要对产地来源和/或解决方案的一些指导:

I am encountering an issue and I ran out of ideas, I need some guidance towards the origin and/or the solution:

服务器端

我加入微软的标准的Web API控制器 Values​​Controller ,这看起来是这样的:

I added the standard Microsoft Web Api Controller class "ValuesController" which looks like this:

public class ValuesController : ApiController
{              
   public string Get(int id){ return "value"; }
   ...

客户端

在我AngularJS控制器功能,我有一个简单的get

In my AngularJS Controller function I have a simple get

$http({method:'GET',url: '/api/values/1'}).success(function(data){
        $scope.value  =data;
    })

中的HTML如下:

The HTML looks like this:

  <input type="text" ng-model="value" />

奇怪的(问题)是我得到:值在我的输入,而不是仅仅值(不含引号)。
为了避免误解,我得到这样的:

The weird thing(the issue) is that I get: "value" in my input instead of just value (no quotes). To avoid misunderstandings, I am getting this:

而不是这样的:

当然,问题是:为什么?如何解决呢*?

And of course the questions are: why?? and how do I fix it*?

*希望的回答将不会返回,而不是一个简单的字符串的对象:)

*hopefully the answer will not be returning an object instead of a simple string :)

推荐答案

我有IM pression,这是由于JSON格式的安全漏洞。在$ http服务的文档有部分所谓的 JSON漏洞防护这表明角度需要几个precautions避免攻击。

I have the impression that this is due to security vulnerabilities in the JSON format. In the documentation of the $http service there is section called JSON Vulnerability Protection that suggests that Angular takes a few precautions to avoid an attack.

他们推荐以下文章阅读:解剖其中,据我所看到再往类似你这样的情况下,一个微妙的JSON漏洞

They recommend the reading of the following article: Anatomy of a Subtle JSON Vulnerability which as far as I can see delves into a case similar to yours.

底线,我想你将不得不回到一个完整的JSON对象,而不是只是一个字符串。另一种方法是,以确保你得到一个JSON对象,通过执行

Bottom line, I think you will have to return a full JSON object and not just a string. An alternative is to make sure you are getting a JSON object, by doing

$scope.value = JSON.parse(value)

这篇关于单值的WebAPI的方法和AngularJs $ HTTP JSON处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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